Algorithm


Problem Name: 925. Long Pressed Name

Your friend is typing his name into a keyboard. Sometimes, when typing a character c, the key might get long pressed, and the character will be typed 1 or more times.

You examine the typed characters of the keyboard. Return True if it is possible that it was your friends name, with some characters (possibly none) being long pressed.

 

Example 1:

Input: name = "alex", typed = "aaleex"
Output: true
Explanation: 'a' and 'e' in 'alex' were long pressed.

Example 2:

Input: name = "saeed", typed = "ssaaedd"
Output: false
Explanation: 'e' must have been pressed twice, but it was not in the typed output.

 

Constraints:

  • 1 <= name.length, typed.length <= 1000
  • name and typed consist of only lowercase English letters.

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming


class Solution {
public:
    bool isLongPressedName(string name, string typed) {
        int a = 0, b = 0, n = name.size(), m = typed.size();
        while (a  <  n && b < m) {
            if (name[a++] != typed[b++]) return false;
            while (b > 0 && name[a] != typed[b] && typed[b] == typed[b - 1]) ++b;
        }
        return a == n && b == m;
    }
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
name = "alex", typed = "aaleex"

Output

x
+
cmd
true

#2 Code Example with Java Programming

Code - Java Programming


class Solution {
  public boolean isLongPressedName(String name, String typed) {
    int idxName = 0;
    int idxTyped = 0;
    while (idxName  <  name.length() && idxTyped < typed.length()) {
      if (name.charAt(idxName) != typed.charAt(idxTyped)) {
        return false;
      }
      char c = name.charAt(idxName);
      int nameFreq = 0;
      while (idxName  <  name.length() && name.charAt(idxName) == c) {
        idxName++;
        nameFreq++;
      }
      c = typed.charAt(idxTyped);
      int typedFreq = 0;
      while (idxTyped  <  typed.length() && typed.charAt(idxTyped) == c) {
        idxTyped++;
        typedFreq++;
      }
      if (nameFreq > typedFreq) {
        return false;
      }
    }
    return idxName == name.length() && idxTyped == typed.length();
  }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
name = "alex", typed = "aaleex"

Output

x
+
cmd
true

#3 Code Example with Javascript Programming

Code - Javascript Programming


const isLongPressedName = function(name, typed) {
  let i = 0, m = name.length, n = typed.length
  for(let j = 0; j  <  n; j++) {
    if(i < m && name[i] === typed[j]) i++
    else if(j === 0 || typed[j] !== typed[j - 1]> return false
  }
  return i === m
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
name = "saeed", typed = "ssaaedd"

Output

x
+
cmd
false

#4 Code Example with Python Programming

Code - Python Programming


class Solution:
    def isLongPressedName(self, name, typed):
        pre, i = None, 0
        for c in typed:
            if i < len(name) and c == name[i]:
                pre, i = name[i], i + 1
            elif c != pre:
                return False
        return i == len(name)
Copy The Code & Try With Live Editor

Input

x
+
cmd
name = "saeed", typed = "ssaaedd"

Output

x
+
cmd
false

#5 Code Example with C# Programming

Code - C# Programming


namespace LeetCode
{
    public class _0925_LongPressedName
    {
        public bool IsLongPressedName(string name, string typed)
        {
            int i = 0, j = 0;
            while (i  <  name.Length && j < typed.Length)
            {
                if (name[i] == typed[j])
                {
                    i++;
                    j++;
                }
                else
                {
                    if (j > 0 && typed[j] == typed[j - 1])
                        j++;
                    else
                        return false;
                }
            }

            while (j  <  typed.Length && j > 0 && typed[j] == typed[j - 1])
                j++;

            return i == name.Length && j == typed.Length;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
name = "alex", typed = "aaleex"

Output

x
+
cmd
true
Advertisements

Demonstration


Previous
#924 Leetcode Minimize Malware Spread Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#926 Leetcode Flip String to Monotone Increasing Solution in C, C++, Java, JavaScript, Python, C# Leetcode