Skip to content

Commit 46b1055

Browse files
committed
Added Check If a String Can Break Another String.java
1 parent d80cd51 commit 46b1055

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public boolean checkIfCanBreak(String s1, String s2) {
3+
int n = s1.length();
4+
int[] counterS1 = new int[26];
5+
int[] counterS2 = new int[26];
6+
for (int idx = 0; idx < n; idx++) {
7+
counterS1[s1.charAt(idx) - 'a']++;
8+
counterS2[s2.charAt(idx) - 'a']++;
9+
}
10+
int countS1 = 0;
11+
int countS2 = 0;
12+
int side = 0;
13+
for (int idx = 0; idx < 26; idx++) {
14+
countS1 += counterS1[idx];
15+
countS2 += counterS2[idx];
16+
if (countS1 > countS2) {
17+
if (side == 1) {
18+
return false;
19+
}
20+
side = -1;
21+
} else if (countS2 > countS1) {
22+
if (side == -1) {
23+
return false;
24+
}
25+
side = 1;
26+
}
27+
}
28+
return true;
29+
}
30+
}

0 commit comments

Comments
 (0)