-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpressiveWords.java
45 lines (40 loc) · 1.48 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
38
39
40
41
42
43
44
45
package solutions;
// [Problem] https://leetcode.com/problems/expressive-words
class ExpressiveWords {
// Two pointers
// O(n * l) time, O(1) space
// where n = number of words, l = string length
public int expressiveWords(String s, String[] words) {
int expressiveWordsCount = 0;
for (String word : words) {
if (isStretchy(s, word)) {
expressiveWordsCount++;
}
}
return expressiveWordsCount;
}
private boolean isStretchy(String s, String word) {
int wordIndex = 0;
for (int i = 0; i < s.length(); i++) {
if (wordIndex < word.length() && s.charAt(i) == word.charAt(wordIndex)) {
wordIndex++;
} else if (i > 0 && i < s.length() - 1 && s.charAt(i) == s.charAt(i - 1) && s.charAt(i) == s.charAt(i + 1)) {
i++;
} else if (i > 1 && s.charAt(i) == s.charAt(i - 1) && s.charAt(i - 1) == s.charAt(i - 2)) {
continue;
} else {
return false;
}
}
return wordIndex == word.length();
}
// Test
public static void main(String[] args) {
ExpressiveWords solution = new ExpressiveWords();
String s = "heeellooo";
String[] words = {"hello", "hi", "helo"};
int expectedOutput = 1;
int actualOutput = solution.expressiveWords(s, words);
System.out.println("Test passed? " + (expectedOutput == actualOutput));
}
}