File tree 3 files changed +90
-0
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -385,6 +385,7 @@ _If you like this project, please leave me a star._ ★
385
385
|819|[ Most Common Word] ( https://leetcode.com/problems/most-common-word/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_819.java ) | |Easy| HashMap
386
386
|814|[ Binary Tree Pruning] ( https://leetcode.com/problems/binary-tree-pruning/ ) |[ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_814.java ) | |Medium| recursion, DFS
387
387
|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|
388
389
| 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|
389
390
| 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|
390
391
| 804| [ Unique Morse Code Words] ( https://leetcode.com/problems/unique-morse-code-words/ ) | [ Solution] ( ../master/src/main/java/com/fishercoder/solutions/_804.java ) | | Easy|
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments