Check If a String Can Break Another String LeetCode solution in C++

1
class Solution {
2
public:
3
bool checkIfCanBreak(string s1, string s2) {
4
vector<int> freq1(26, 0), freq2(26, 0);
5
for (int i = 0; i < s1.size(); i++) {
6
freq1[s1[i] - 'a']++;
7
}
8
for (int i = 0; i < s2.size(); i++) {
9
freq2[s2[i] - 'a']++;
10
}
11
int count1 = 0, count2 = 0;
12
bool check1 = true, check2 = true;
13
for (int i = 0; i < 25; i++) {
14
count1 += freq1[i] - freq2[i];
15
count2 += freq2[i] - freq1[i];
16
if (count1 < 0) {
17
check1 = false;
18
}
19
if (count2 < 0) {
20
check2 = false;
21
}
22
if (count1 < 0 && count2 < 0) {
23
break;
24
}
25
}
26
return check1 || check2;
27
}
28
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0