forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressive Words.java
More file actions
47 lines (37 loc) · 1.11 KB
/
Expressive Words.java
File metadata and controls
47 lines (37 loc) · 1.11 KB
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
46
47
class Solution {
public int expressiveWords(String S, String[] words) {
int count = 0;
char[] sChar = S.toCharArray();
for (String word : words) {
char[] wChar = word.toCharArray();
if (check(sChar, wChar)) {
count++;
}
}
return count;
}
private boolean check(char[] s, char[] w) {
int i = 0;
int j = 0;
while (i < s.length && j < w.length) {
if (s[i] != w[j]) {
return false;
}
int tempI = i;
int tempJ = j;
while (i < s.length && s[i] == s[tempI]) {
i++;
}
while (j < w.length && w[j] == w[tempJ]) {
j++;
}
int l1 = i - tempI;
int l2 = j - tempJ;
if (l1 == l2 || l1 >= 3 && l1 > l2) {
continue;
}
return false;
}
return i == s.length && j == w.length;
}
}