-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressiveWords.java
37 lines (32 loc) · 1.19 KB
/
ExpressiveWords.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
package gracefulsoul.problems;
public class ExpressiveWords {
// https://leetcode.com/problems/expressive-words/submissions/880490942/
public static void main(String[] args) {
ExpressiveWords test = new ExpressiveWords();
System.out.println(test.expressiveWords("heeellooo", new String[] { "hello", "hi", "helo" }));
System.out.println(test.expressiveWords("zzzzzyyyyy", new String[] { "zzyy", "zy", "zyy" }));
}
public int expressiveWords(String s, String[] words) {
int result = 0;
for (String word : words) {
if (this.isPossible(s.toCharArray(), word.toCharArray())) {
result++;
}
}
return result;
}
private boolean isPossible(char[] sCharArray, char[] wordCharArray) {
int sLength = sCharArray.length;
int wordLength = wordCharArray.length;
int j = 0;
for (int i = 0; i < sLength; i++) {
if (j < wordLength && sCharArray[i] == wordCharArray[j]) {
j++;
} else if (!(0 < i && i < sLength - 1 && sCharArray[i - 1] == sCharArray[i] && sCharArray[i] == sCharArray[i + 1])
&& !(1 < i && sCharArray[i - 2] == sCharArray[i - 1] && sCharArray[i - 1] == sCharArray[i])) {
return false;
}
}
return j == wordLength;
}
}