Skip to content

Commit 13a3e2a

Browse files
committed
feat: add solutions to lc problem: No.2124
No.2124.Check if All A's Appears Before All B's
1 parent ccc716c commit 13a3e2a

File tree

6 files changed

+77
-4
lines changed

6 files changed

+77
-4
lines changed

solution/2100-2199/2124.Check if All A's Appears Before All B's/README.md

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,58 @@
4949

5050
<!-- 这里可写通用的实现逻辑 -->
5151

52+
**方法一:模拟**
53+
54+
根据题意,字符串 $s$ 仅由字符 `a`, `b` 组成。
55+
56+
要使得所有 `a` 都在 `b` 之前出现,需要满足 `b` 之后不会出现 `a`,也就是说,字符串 "ba" 不是字符串 $s$ 的子串,条件才能成立。
57+
58+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 是字符串 $s$ 的长度。
59+
5260
<!-- tabs:start -->
5361

5462
### **Python3**
5563

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

5866
```python
59-
67+
class Solution:
68+
def checkString(self, s: str) -> bool:
69+
return "ba" not in s
6070
```
6171

6272
### **Java**
6373

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

6676
```java
77+
class Solution {
78+
public boolean checkString(String s) {
79+
return !s.contains("ba");
80+
}
81+
}
82+
```
6783

84+
### **C++**
85+
86+
```cpp
87+
class Solution {
88+
public:
89+
bool checkString(string s) {
90+
return s.find("ba") == string::npos;
91+
}
92+
};
6893
```
6994
70-
### **TypeScript**
95+
### **Go**
7196
72-
<!-- 这里可写当前语言的特殊实现逻辑 -->
97+
```go
98+
func checkString(s string) bool {
99+
return !strings.Contains(s, "ba")
100+
}
101+
```
102+
103+
### **TypeScript**
73104

74105
```ts
75106

solution/2100-2199/2124.Check if All A's Appears Before All B's/README_EN.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,38 @@ There are no &#39;a&#39;s, hence, every &#39;a&#39; appears before every &#39;b&
5151
### **Python3**
5252

5353
```python
54-
54+
class Solution:
55+
def checkString(self, s: str) -> bool:
56+
return "ba" not in s
5557
```
5658

5759
### **Java**
5860

5961
```java
62+
class Solution {
63+
public boolean checkString(String s) {
64+
return !s.contains("ba");
65+
}
66+
}
67+
```
68+
69+
### **C++**
70+
71+
```cpp
72+
class Solution {
73+
public:
74+
bool checkString(string s) {
75+
return s.find("ba") == string::npos;
76+
}
77+
};
78+
```
79+
80+
### **Go**
6081
82+
```go
83+
func checkString(s string) bool {
84+
return !strings.Contains(s, "ba")
85+
}
6186
```
6287

6388
### **TypeScript**
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
bool checkString(string s) {
4+
return s.find("ba") == string::npos;
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func checkString(s string) bool {
2+
return !strings.Contains(s, "ba")
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public boolean checkString(String s) {
3+
return !s.contains("ba");
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def checkString(self, s: str) -> bool:
3+
return "ba" not in s

0 commit comments

Comments
 (0)