-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution809.java
45 lines (39 loc) · 1.47 KB
/
Solution809.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package leetcode.doublepointer;
public class Solution809 {
public static void main(String[] args) {
System.out.println(new Solution809().expressiveWords("heeellooo", new String[]{"hello", "hi", "helo"}));
}
public int expressiveWords(String s, String[] words) {
int res = 0;
for (String word : words) {
if (isStretchy(s, word)) {
res++;
}
}
return res;
}
private boolean isStretchy(String origin, String word) {
int originBegin = 0, originEnd = 0;
int wordBegin = 0, wordEnd = 0;
while (originEnd < origin.length() && wordEnd < word.length()) {
if (origin.charAt(originEnd) != word.charAt(wordEnd)) {
return false;
}
while (originEnd + 1 < origin.length() && origin.charAt(originEnd) == origin.charAt(originEnd + 1)) {
originEnd++;
}
while (wordEnd + 1 < word.length() && word.charAt(wordEnd) == word.charAt(wordEnd + 1)) {
wordEnd++;
}
if (wordEnd - wordBegin > originEnd - originBegin) {
return false;
}
if (wordEnd - wordBegin < originEnd - originBegin && originEnd - originBegin + 1 < 3) {
return false;
}
originBegin = ++originEnd;
wordBegin = ++wordEnd;
}
return originEnd == origin.length() && wordEnd == word.length();
}
}