leetcode-cn Daily Challenge on October 21th, 2020.
Difficulty : Easy
Related Topics : Two Pointers、String
Your friend is typing his
name
into a keyboard. Sometimes, when typing a characterc
, the key might get long pressed, and the character will be typed 1 or more times.You examine the
typed
characters of the keyboard. ReturnTrue
if it is possible that it was your friends name, with some characters (possibly none) being long pressed.Input: name = "alex", typed = "aaleex" Output: true Explanation: 'a' and 'e' in 'alex' were long pressed.
Input: name = "saeed", typed = "ssaaedd" Output: false Explanation: 'e' must have been pressed twice, but it wasn't in the typed output.
Input: name = "leelee", typed = "lleeelee" Output: true
Input: name = "laiden", typed = "laiden" Output: true Explanation: It's not necessary to long press any character.
1 <= name.length <= 1000
1 <= typed.length <= 1000
- The characters of
name
andtyped
are lowercase letters.
- mine
- Java
-
Two Pointer
Runtime: 0 ms, faster than 100.00%, Memory Usage: 37.4 MB, less than 8.33% of Java online submissions
// O(T)time // O(1)space // T = typed.length(); public boolean isLongPressedName(String name, String typed) { int len1 = name.length(); int len2 = typed.length(); if (len2 < len1) { return false; } int i = 0, j = 0; while (i <= len1 && j < len2){ if(i < len1 && name.charAt(i) == typed.charAt(j)){ i++; j++; }else if (i > 0 && name.charAt(i - 1) == typed.charAt(j)){ j++; }else { break; } } return i == len1 && j ==len2; }
-
LinkedList
Runtime: 1 ms, faster than 50.93%, Memory Usage: 37.4 MB, less than 8.33% of Java online submissions
// O(T)time // O(N)space // N = name.length() T = typed.length(); public boolean isLongPressedName(String name, String typed) { int len1 = name.length(); int len2 = typed.length(); if (len2 < len1) { return false; } LinkedList<Character> t = new LinkedList<>(); for (int i = 0; i < len1; i++) { t.add(name.charAt(i)); } Character removed = 0; int i = len2 - 1; for (; i >= 0; i--) { if (!t.isEmpty() && typed.charAt(i) == t.getLast()) { removed = t.removeLast(); } else if (removed != typed.charAt(i)) { break; } } return t.isEmpty() && i == -1; }
-
- Java
- the most votes
Two Pointer
Runtime: 0 ms, faster than 100.00%, Memory Usage: 37.6 MB, less than 8.33% of Java online submissions
// O(T)time // O(1)space // T = typed.length(); public boolean isLongPressedName(String name, String typed) { int i = 0, m = name.length(), n = typed.length(); for (int j = 0; j < n; ++j) if (i < m && name.charAt(i) == typed.charAt(j)) ++i; else if (j == 0 || typed.charAt(j) != typed.charAt(j - 1)) return false; return i == m; }
- the leetcode's solution
- Group into Blocks
Runtime: 1 ms, faster than 50.93%, Memory Usage: 37.5 MB, less than 8.33% of Java online submissions
class Solution { public boolean isLongPressedName(String name, String typed) { Group g1 = groupify(name); Group g2 = groupify(typed); if (!g1.key.equals(g2.key)) return false; for (int i = 0; i < g1.count.size(); ++i) if (g1.count.get(i) > g2.count.get(i)) return false; return true; } public Group groupify(String S) { StringBuilder key = new StringBuilder(); List<Integer> count = new ArrayList(); int anchor = 0; int N = S.length(); for (int i = 0; i < N; ++i) { if (i == N-1 || S.charAt(i) != S.charAt(i+1)) { // end of group key.append(S.charAt(i)); count.add(i - anchor + 1); anchor = i+1; } } return new Group(key.toString(), count); } } class Group { String key; List<Integer> count; Group(String k, List<Integer> c) { key = k; count = c; } }