https://leetcode.com/problems/long-pressed-name/
Approach 2: Two Pointer
X. https://zhanghuimeng.github.io/post/leetcode-925-long-pressed-name-and-weekly-contest-107/
https://www.acwing.com/solution/leetcode/content/584/
同时从前往后遍历两个字符串,每次分别找出两个字符串中相同字母的一段,如果:
字母相同;
name中该段的长度小于等于typed中该段的长度;
则说明该段字母合法,否则说明该段字母不合法。
如果所有相同字母段都合法,则返回true,否则返回false。
https://leetcode.com/problems/long-pressed-name/discuss/183929/C++-2-lines-accepted-and-5-lines-accurate
http://www.noteanddata.com/leetcode-925-Long-Pressed-Name-solution-note.html
X. https://zhuanlan.zhihu.com/p/50564246
X.
https://blog.csdn.net/fuxuemingzhu/article/details/83245395
思想是,使用两个指针,分别指向名字和输入字符串,然后判断对应位置是否能够对应的上。具体做法是统计两个字符串中相同的字符串重复出现了多少次。我用一个变量指向name,每次向后移动,在每次开始的时候需要保存这个字符,然后我们需要找一下每个字符串后面有多少个相同的字符。最后需要判断,如果输入的这个字符的个数小于名字里面有的,那么就是输入错误了。当所有的判断都结束没有返回错误,那么就是成功了。
def isLongPressedName(self, name, typed):
"""
:type name: str
:type typed: str
:rtype: bool
"""
M = len(name)
N = len(typed)
i, j = 0, 0
while i < M:
c_i = name[i]
count_i = 0
count_j = 0
while i < M and name[i] == c_i:
i += 1
count_i += 1
while j < N and typed[j] == c_i:
j += 1
count_j += 1
if count_j < count_i:
return False
return True
https://leetcode.com/problems/long-pressed-name/discuss/183965/Short-Java-Solution
TODO
https://leetcode.com/problems/long-pressed-name/discuss/184488/Java-one-pass-solution-4ms-with-O(1)-extra-space
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.
Approach 1: Group into Blocks
For a string like
S = 'aabbbbccc'
, we can group it into blocks groupify(S) = [('a', 2), ('b', 4), ('c', 3)]
, that consist of a key 'abc'
and a count [2, 4, 3]
.
Then, the necessary and sufficient condition for
typed
to be a long-pressed version of name
is that the keys are the same, and each entry of the count of typed
is at least the entry for the count of name
.
For example,
'aaleex'
is a long-pressed version of 'alex'
: because when considering the groups [('a', 2), ('l', 1), ('e', 2), ('x', 1)]
and [('a', 1), ('l', 1), ('e', 1), ('x', 1)]
, they both have the key 'alex'
, and the count [2,1,2,1]
is at least [1,1,1,1]
when making an element-by-element comparison (2 >= 1, 1 >= 1, 2 >= 1, 1 >= 1)
.
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;
}
}
Approach 2: Two Pointer
As in Approach 1, we want to check the key and the count. We can do this on the fly.
Suppose we read through the characters
name
, and eventually it doesn't match typed
.
There are some cases for when we are allowed to skip characters of
typed
. Let's use a tuple to denote the case (name
, typed
):- In a case like
('aab', 'aaaaab')
, we can skip the 3rd, 4th, and 5th'a'
intyped
because we have already processed an'a'
in this block. - In a case like
('a', 'b')
, we can't skip the 1st'b'
intyped
because we haven't processed anything in the current block yet.
Algorithm
This leads to the following algorithm:
- For each character in
name
, if there's a mismatch with the next character intyped
:- If it's the first character of the block in
typed
, the answer isFalse
. - Else, discard all similar characers of
typed
coming up. The next (different) character coming must match.
- If it's the first character of the block in
Also, we'll keep track on the side of whether we are at the first character of the block.
public boolean isLongPressedName(String name, String typed) {
int j = 0;
for (char c : name.toCharArray()) {
if (j == typed.length())
return false;
// If mismatch...
if (typed.charAt(j) != c) {
// If it's the first char of the block, ans is false.
if (j == 0 || typed.charAt(j - 1) != typed.charAt(j))
return false;
// Discard all similar chars
char cur = typed.charAt(j);
while (j < typed.length() && typed.charAt(j) == cur)
j++;
// If next isn't a match, ans is false.
if (j == typed.length() || typed.charAt(j) != c)
return false;
}
j++;
}
return true;
}
X. https://zhanghuimeng.github.io/post/leetcode-925-long-pressed-name-and-weekly-contest-107/
https://www.acwing.com/solution/leetcode/content/584/
同时从前往后遍历两个字符串,每次分别找出两个字符串中相同字母的一段,如果:
字母相同;
name中该段的长度小于等于typed中该段的长度;
则说明该段字母合法,否则说明该段字母不合法。
如果所有相同字母段都合法,则返回true,否则返回false。
https://leetcode.com/problems/long-pressed-name/discuss/183929/C++-2-lines-accepted-and-5-lines-accurate
bool isLongPressedName(string name, string typed) { int n = name.size(), m = typed.size(); if (n > m) return false; int i = 0, j = 0; while (i < n || j < m) { if (i < n && typed[j] == name[i]) i++, j++; else if (i > 0 && typed[j] == name[i - 1]) j++; else break; } if (i == n && j == m) return true; return false; }https://leetcode.com/problems/long-pressed-name/discuss/183994/C%2B%2BJavaPython-Two-Pointers
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;
}
http://www.noteanddata.com/leetcode-925-Long-Pressed-Name-solution-note.html
public boolean isLongPressedName(String name, String typed) {
int i = 0, j = 0;
while(i < name.length() && j < typed.length()) {
if(name.charAt(i) == typed.charAt(j)) {
i++;
j++;
}
else {
if(j == 0 || typed.charAt(j) != typed.charAt(j-1)) return false;
j++;
}
}
if(i != name.length()) return false;
while(j < typed.length()) {
if(typed.charAt(j) != typed.charAt(j-1)) return false;
j++;
}
return true;
}
这个题目是对两个字符串进行操作,很容易想到双指针,如果name[i]和typed[j]相等,那么就都往下走一步,当不相等时,由于长按,如果typed[j] == typed[j-1],可以让j++。 最后i要等于name.length。
X.
https://blog.csdn.net/fuxuemingzhu/article/details/83245395
思想是,使用两个指针,分别指向名字和输入字符串,然后判断对应位置是否能够对应的上。具体做法是统计两个字符串中相同的字符串重复出现了多少次。我用一个变量指向name,每次向后移动,在每次开始的时候需要保存这个字符,然后我们需要找一下每个字符串后面有多少个相同的字符。最后需要判断,如果输入的这个字符的个数小于名字里面有的,那么就是输入错误了。当所有的判断都结束没有返回错误,那么就是成功了。
def isLongPressedName(self, name, typed):
"""
:type name: str
:type typed: str
:rtype: bool
"""
M = len(name)
N = len(typed)
i, j = 0, 0
while i < M:
c_i = name[i]
count_i = 0
count_j = 0
while i < M and name[i] == c_i:
i += 1
count_i += 1
while j < N and typed[j] == c_i:
j += 1
count_j += 1
if count_j < count_i:
return False
return True
https://leetcode.com/problems/long-pressed-name/discuss/183965/Short-Java-Solution
public boolean isLongPressedName(String nameStr, String typeStr) {
char[] name = nameStr.toCharArray(), typed = typeStr.toCharArray();
int n = 0, t = 0;
while (n < name.length && t < typed.length) {
int need = 1;
char c = name[n++];
while (n < name.length && c == name[n]) {
n++;
need++;
}
while (t < typed.length && typed[t] == c) {
need--;
t++;
}
if (need > 0)
return false;
}
return n == name.length && t == typed.length;
}
X.TODO
https://leetcode.com/problems/long-pressed-name/discuss/184488/Java-one-pass-solution-4ms-with-O(1)-extra-space
public boolean isLongPressedName(String name, String typed) {
int difference = 0;
for (int i = 0; i < typed.length();) {
//letters are equal -> go next
if (difference <= i && i - difference < name.length() && typed.charAt(i) == name.charAt(i - difference)) {
i++;
}
// letters are not equal, but we can link typed letter to name letter from the previous iteration
else if (difference < i && i - difference - 1 < name.length() && typed.charAt(i) == name.charAt(i - difference - 1)) {
difference++;
} else return false;
}
// check that at the end of name there's no odd symbols
return typed.length() - difference == name.length();
}
X. 暴力的做法bool isLongPressedName(string name, string typed) { vector<pair<char, int>> press1, press2; for (char ch: name) { if (press1.size() == 0 || press1.back().first != ch) press1.emplace_back(ch, 1); else press1.back().second++; } for (char ch: typed) { if (press2.size() == 0 || press2.back().first != ch) press2.emplace_back(ch, 1); else press2.back().second++; } if (press1.size() != press2.size()) return false; for (int i = 0; i < press1.size(); i++) if (press1[i] > press2[i]) return false; return true; }