Skip to content

Commit 9e4f989

Browse files
committed
feat: add solutions to lc problem: No.1433
No.1433.Check If a String Can Break Another String
1 parent e797b43 commit 9e4f989

File tree

9 files changed

+197
-5
lines changed

9 files changed

+197
-5
lines changed

solution/0100-0199/0161.One Edit Distance/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ def isOneEditDistance(self, s: str, t: str) -> bool:
77
return False
88
for i, c in enumerate(t):
99
if c != s[i]:
10-
return s[i + 1:] == t[i + 1:] if m == n else s[i + 1:] == t[i:]
10+
return s[i + 1 :] == t[i + 1 :] if m == n else s[i + 1 :] == t[i:]
1111
return m == n + 1

solution/1200-1299/1282.Group the People Given the Group Size They Belong To/Solution.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ def groupThePeople(self, groupSizes: List[int]) -> List[List[int]]:
33
g = defaultdict(list)
44
for i, v in enumerate(groupSizes):
55
g[v].append(i)
6-
return [v[j: j + i] for i, v in g.items() for j in range(0, len(v), i)]
6+
return [v[j : j + i] for i, v in g.items() for j in range(0, len(v), i)]

solution/1200-1299/1286.Iterator for Combination/Solution.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
class CombinationIterator:
2-
32
def __init__(self, characters: str, combinationLength: int):
43
self.curr = (1 << len(characters)) - 1
54
self.size = combinationLength

solution/1400-1499/1433.Check If a String Can Break Another String/README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,93 @@
4747

4848
<!-- 这里可写通用的实现逻辑 -->
4949

50+
**方法一:排序**
51+
52+
将字符串 $s1$, $s2$ 分别进行升序排序。然后同时遍历两个字符串,对应字符进行大小比较。若对于任意 $i∈[0, n),都有$ $s1[i] \le s2[i]$,或者都有 $s1[i] \ge s2[i]$,则存在一个排列可以打破另一个排列。
53+
54+
时间复杂度 $O(nlogn)$。
55+
5056
<!-- tabs:start -->
5157

5258
### **Python3**
5359

5460
<!-- 这里可写当前语言的特殊实现逻辑 -->
5561

5662
```python
57-
63+
class Solution:
64+
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
65+
cs1 = sorted(s1)
66+
cs2 = sorted(s2)
67+
return all(a >= b for a, b in zip(cs1, cs2)) or all(
68+
a <= b for a, b in zip(cs1, cs2)
69+
)
5870
```
5971

6072
### **Java**
6173

6274
<!-- 这里可写当前语言的特殊实现逻辑 -->
6375

6476
```java
77+
class Solution {
78+
public boolean checkIfCanBreak(String s1, String s2) {
79+
char[] cs1 = s1.toCharArray();
80+
char[] cs2 = s2.toCharArray();
81+
Arrays.sort(cs1);
82+
Arrays.sort(cs2);
83+
return check(cs1, cs2) || check(cs2, cs1);
84+
}
85+
86+
private boolean check(char[] cs1, char[] cs2) {
87+
for (int i = 0; i < cs1.length; ++i) {
88+
if (cs1[i] < cs2[i]) {
89+
return false;
90+
}
91+
}
92+
return true;
93+
}
94+
}
95+
```
96+
97+
### **C++**
98+
99+
```cpp
100+
class Solution {
101+
public:
102+
bool checkIfCanBreak(string s1, string s2) {
103+
sort(s1.begin(), s1.end());
104+
sort(s2.begin(), s2.end());
105+
return check(s1, s2) || check(s2, s1);
106+
}
107+
108+
bool check(string& s1, string& s2) {
109+
for (int i = 0; i < s1.size(); ++i) {
110+
if (s1[i] < s2[i]) {
111+
return false;
112+
}
113+
}
114+
return true;
115+
}
116+
};
117+
```
65118

119+
### **Go**
120+
121+
```go
122+
func checkIfCanBreak(s1 string, s2 string) bool {
123+
cs1 := []byte(s1)
124+
cs2 := []byte(s2)
125+
sort.Slice(cs1, func(i, j int) bool { return cs1[i] < cs1[j] })
126+
sort.Slice(cs2, func(i, j int) bool { return cs2[i] < cs2[j] })
127+
check := func(cs1, cs2 []byte) bool {
128+
for i := range cs1 {
129+
if cs1[i] < cs2[i] {
130+
return false
131+
}
132+
}
133+
return true
134+
}
135+
return check(cs1, cs2) || check(cs2, cs1)
136+
}
66137
```
67138

68139
### **...**

solution/1400-1499/1433.Check If a String Can Break Another String/README_EN.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,78 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
54+
cs1 = sorted(s1)
55+
cs2 = sorted(s2)
56+
return all(a >= b for a, b in zip(cs1, cs2)) or all(
57+
a <= b for a, b in zip(cs1, cs2)
58+
)
5359
```
5460

5561
### **Java**
5662

5763
```java
64+
class Solution {
65+
public boolean checkIfCanBreak(String s1, String s2) {
66+
char[] cs1 = s1.toCharArray();
67+
char[] cs2 = s2.toCharArray();
68+
Arrays.sort(cs1);
69+
Arrays.sort(cs2);
70+
return check(cs1, cs2) || check(cs2, cs1);
71+
}
72+
73+
private boolean check(char[] cs1, char[] cs2) {
74+
for (int i = 0; i < cs1.length; ++i) {
75+
if (cs1[i] < cs2[i]) {
76+
return false;
77+
}
78+
}
79+
return true;
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
bool checkIfCanBreak(string s1, string s2) {
90+
sort(s1.begin(), s1.end());
91+
sort(s2.begin(), s2.end());
92+
return check(s1, s2) || check(s2, s1);
93+
}
94+
95+
bool check(string& s1, string& s2) {
96+
for (int i = 0; i < s1.size(); ++i) {
97+
if (s1[i] < s2[i]) {
98+
return false;
99+
}
100+
}
101+
return true;
102+
}
103+
};
104+
```
58105

106+
### **Go**
107+
108+
```go
109+
func checkIfCanBreak(s1 string, s2 string) bool {
110+
cs1 := []byte(s1)
111+
cs2 := []byte(s2)
112+
sort.Slice(cs1, func(i, j int) bool { return cs1[i] < cs1[j] })
113+
sort.Slice(cs2, func(i, j int) bool { return cs2[i] < cs2[j] })
114+
check := func(cs1, cs2 []byte) bool {
115+
for i := range cs1 {
116+
if cs1[i] < cs2[i] {
117+
return false
118+
}
119+
}
120+
return true
121+
}
122+
return check(cs1, cs2) || check(cs2, cs1)
123+
}
59124
```
60125

61126
### **...**
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool checkIfCanBreak(string s1, string s2) {
4+
sort(s1.begin(), s1.end());
5+
sort(s2.begin(), s2.end());
6+
return check(s1, s2) || check(s2, s1);
7+
}
8+
9+
bool check(string& s1, string& s2) {
10+
for (int i = 0; i < s1.size(); ++i) {
11+
if (s1[i] < s2[i]) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func checkIfCanBreak(s1 string, s2 string) bool {
2+
cs1 := []byte(s1)
3+
cs2 := []byte(s2)
4+
sort.Slice(cs1, func(i, j int) bool { return cs1[i] < cs1[j] })
5+
sort.Slice(cs2, func(i, j int) bool { return cs2[i] < cs2[j] })
6+
check := func(cs1, cs2 []byte) bool {
7+
for i := range cs1 {
8+
if cs1[i] < cs2[i] {
9+
return false
10+
}
11+
}
12+
return true
13+
}
14+
return check(cs1, cs2) || check(cs2, cs1)
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public boolean checkIfCanBreak(String s1, String s2) {
3+
char[] cs1 = s1.toCharArray();
4+
char[] cs2 = s2.toCharArray();
5+
Arrays.sort(cs1);
6+
Arrays.sort(cs2);
7+
return check(cs1, cs2) || check(cs2, cs1);
8+
}
9+
10+
private boolean check(char[] cs1, char[] cs2) {
11+
for (int i = 0; i < cs1.length; ++i) {
12+
if (cs1[i] < cs2[i]) {
13+
return false;
14+
}
15+
}
16+
return true;
17+
}
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def checkIfCanBreak(self, s1: str, s2: str) -> bool:
3+
cs1 = sorted(s1)
4+
cs2 = sorted(s2)
5+
return all(a >= b for a, b in zip(cs1, cs2)) or all(
6+
a <= b for a, b in zip(cs1, cs2)
7+
)

0 commit comments

Comments
 (0)