Skip to content

Commit cb0df72

Browse files
authored
added solution and test case for 809 (fishercoder1534#149)
1 parent e8c75f4 commit cb0df72

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ _If you like this project, please leave me a star._ ★
385385
|819|[Most Common Word](https://leetcode.com/problems/most-common-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_819.java) | |Easy| HashMap
386386
|814|[Binary Tree Pruning](https://leetcode.com/problems/binary-tree-pruning/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_814.java) | |Medium| recursion, DFS
387387
|811|[Subdomain Visit Count](https://leetcode.com/problems/subdomain-visit-count/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_811.java) | |Easy| HashMap
388+
|809|[Expressive Words](https://leetcode.com/problems/expressive-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_809.java) | |Medium|
388389
|807|[Max Increase to Keep City Skyline](https://leetcode.com/problems/max-increase-to-keep-city-skyline/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_807.java) | |Medium|
389390
|806|[Number of Lines To Write String](https://leetcode.com/problems/number-of-lines-to-write-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_806.java) | |Easy|
390391
|804|[Unique Morse Code Words](https://leetcode.com/problems/unique-morse-code-words/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_804.java) | |Easy|
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _809 {
4+
public static class Solution1 {
5+
public int expressiveWords (String S, String[] words ) {
6+
int ans = 0;
7+
for (String w : words) {
8+
if (check(S, w)) {
9+
ans++;
10+
}
11+
}
12+
return ans;
13+
}
14+
private boolean check (String S, String w) {
15+
int i = 0;
16+
int j = 0;
17+
/* Logic is to check whether character at same index of S and w are same
18+
if same,
19+
1. Find the consecutive number of occurrences of the char in S (say len1) and w ( say len2)
20+
2. If len1 == len 2 , move to the next char in S and w
21+
3. If len1 >= 3 and len2 < len1, means we can make the char in w stretchy to match len1
22+
4. else, return false, because it's not possible to stretch the char in w
23+
*/
24+
while (i < S.length() && j < w.length()) {
25+
char ch1 = S.charAt(i);
26+
char ch2 = w.charAt(j);
27+
28+
int len1 = getLen(S, i);
29+
int len2 = getLen(w, j);
30+
if (ch1 == ch2) {
31+
if (len1 == len2) {
32+
i = i + len1;
33+
j = j + len2;
34+
}
35+
else if (len1 >= 3 && len2 < len1) {
36+
i = i + len1;
37+
j = j + len2;
38+
}
39+
else {
40+
return false;
41+
}
42+
}
43+
else {
44+
return false;
45+
}
46+
}
47+
return i == S.length() && j == w.length();
48+
}
49+
50+
private int getLen (String value, int i) {
51+
i = i + 1;
52+
int count = 1;
53+
for(int j = i; j<value.length(); j++) {
54+
if(value.charAt(j) == value.charAt(i-1)) {
55+
count++;
56+
}
57+
else {
58+
break;
59+
}
60+
}
61+
return count;
62+
}
63+
}
64+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._809;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class _809Test {
10+
private static _809.Solution1 solution1;
11+
private String[] words;
12+
private String S;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _809.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
words = new String[] {"hello", "hi", "helo"};
22+
S = "heeellooo";
23+
assertEquals(1, solution1.expressiveWords(S, words));
24+
}
25+
}

0 commit comments

Comments
 (0)