forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-if-a-string-can-break-another-string.cpp
74 lines (68 loc) · 1.74 KB
/
check-if-a-string-can-break-another-string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Time: O(n)
// Space: O(1)
class Solution {
public:
bool checkIfCanBreak(string s1, string s2) {
const auto& count1 = counter(s1), &count2 = counter(s2);
return isBreak(count1, count2) || isBreak(count2, count1);
}
private:
bool isBreak(const vector<int>& count1, const const vector<int>& count2) {
int curr1 = 0, curr2 = 0;
for (int c = 0; c < 26; ++c) {
curr1 += count1[c];
curr2 += count2[c];
if (curr1 < curr2) {
return false;
}
}
return true;
}
vector<int> counter(const string& word) {
vector<int> count(26);
for (const auto& c : word) {
++count[c - 'a'];
}
return count;
}
};
// Time: O(nlogn)
// Space: O(1)
class Solution2 {
public:
bool checkIfCanBreak(string s1, string s2) {
sort(begin(s1), end(s1)), sort(begin(s2), end(s2));
bool flag1 = true, flag2 = true;
for (int i = 0; i < s1.length(); ++i) {
if (s1[i] < s2[i]) {
flag1 = false;
}
if (s2[i] < s1[i]) {
flag2 = false;
}
if (!flag1 && !flag2) {
return false;
}
}
return true;
}
};
// Time: O(nlogn)
// Space: O(1)
class Solution3 {
public:
bool checkIfCanBreak(string s1, string s2) {
sort(begin(s1), end(s1)), sort(begin(s2), end(s2));
return isBreak(s1, s2) || isBreak(s2, s1);
}
private:
bool isBreak(const string& s1, const string& s2) {
int i = 0;
for (; i < s1.length(); ++i) {
if (!(s1[i] >= s2[i])) {
break;
}
}
return (i == s1.length());
}
};