Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update Solution.java
  • Loading branch information
ThanhNIT committed Jan 31, 2022
commit 7224c8a62cf3f1ce9234e6de6245762e38586c63
19 changes: 8 additions & 11 deletions src/main/java/g0801_0900/s0809_expressive_words/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// #Medium #Array #String #Two_Pointers

public class Solution {
public int expressiveWords(String S, String[] words) {
public int expressiveWords(String s, String[] words) {
int ans = 0;
for (String w : words) {
if (check(S, w)) {
if (check(s, w)) {
ans++;
}
}
return ans;
}

private boolean check(String S, String w) {
private boolean check(String s, String w) {
int i = 0;
int j = 0;
/* Logic is to check whether character at same index of S and w are same
Expand All @@ -23,17 +23,14 @@ private boolean check(String S, String w) {
3. If len1 >= 3 and len2 < len1, means we can make the char in w stretchy to match len1
4. else, return false, because it's not possible to stretch the char in w
*/
while (i < S.length() && j < w.length()) {
char ch1 = S.charAt(i);
while (i < s.length() && j < w.length()) {
char ch1 = s.charAt(i);
char ch2 = w.charAt(j);

int len1 = getLen(S, i);
int len1 = getLen(s, i);
int len2 = getLen(w, j);
if (ch1 == ch2) {
if (len1 == len2) {
i = i + len1;
j = j + len2;
} else if (len1 >= 3 && len2 < len1) {
if (len1 == len2 || (len1 >= 3 && len2 < len1)) {
i = i + len1;
j = j + len2;
} else {
Expand All @@ -43,7 +40,7 @@ private boolean check(String S, String w) {
return false;
}
}
return i == S.length() && j == w.length();
return i == s.length() && j == w.length();
}

private int getLen(String value, int i) {
Expand Down