Skip to content

Commit 3028706

Browse files
committed
feat: add solutions to lc problem: No.1003
No.1003.Check If Word Is Valid After Substitutions
1 parent be68ce1 commit 3028706

File tree

6 files changed

+219
-2
lines changed

6 files changed

+219
-2
lines changed

solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,105 @@
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59+
**方法一:栈模拟**
60+
61+
先判断字符串长度是否为 $3$ 的倍数,若不是直接返回 `false`
62+
63+
接下来我们使用栈模拟操作,遍历字符串 $s$ 的每个字符 $c$:
64+
65+
若 $c$ 等于 `'c'`,且栈顶的两个元素分别为 `'a'``'b'`,则将栈顶的两个元素出栈;否则将 $c$ 入栈。
66+
67+
最后判断栈是否为空,若为空则返回 `true`,否则返回 `false`
68+
69+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
70+
5971
<!-- tabs:start -->
6072

6173
### **Python3**
6274

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

6577
```python
66-
78+
class Solution:
79+
def isValid(self, s: str) -> bool:
80+
if len(s) % 3:
81+
return False
82+
stk = []
83+
for c in s:
84+
if c == 'c' and len(stk) > 1 and stk[-2] == 'a' and stk[-1] == 'b':
85+
stk = stk[:-2]
86+
else:
87+
stk.append(c)
88+
return not stk
6789
```
6890

6991
### **Java**
7092

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

7395
```java
96+
class Solution {
97+
public boolean isValid(String s) {
98+
if (s.length() % 3 > 0) {
99+
return false;
100+
}
101+
StringBuilder stk = new StringBuilder();
102+
for (char c : s.toCharArray()) {
103+
int n = stk.length();
104+
if (c == 'c' && n > 1 && stk.charAt(n - 2) == 'a' && stk.charAt(n - 1) == 'b') {
105+
stk.deleteCharAt(n - 1);
106+
stk.deleteCharAt(n - 2);
107+
} else {
108+
stk.append(c);
109+
}
110+
}
111+
return stk.length() == 0;
112+
}
113+
}
114+
```
115+
116+
### **C++**
117+
118+
```cpp
119+
class Solution {
120+
public:
121+
bool isValid(string s) {
122+
if (s.size() % 3) {
123+
return false;
124+
}
125+
string stk;
126+
for (char c : s) {
127+
int n = stk.size();
128+
if (c == 'c' && n > 1 && stk[n - 2] == 'a' && stk[n - 1] == 'b') {
129+
stk.pop_back();
130+
stk.pop_back();
131+
} else {
132+
stk.push_back(c);
133+
}
134+
}
135+
return stk.empty();
136+
}
137+
};
138+
```
74139
140+
### **Go**
141+
142+
```go
143+
func isValid(s string) bool {
144+
if len(s)%3 > 0 {
145+
return false
146+
}
147+
stk := []rune{}
148+
for _, c := range s {
149+
n := len(stk)
150+
if c == 'c' && n > 1 && stk[n-2] == 'a' && stk[n-1] == 'b' {
151+
stk = stk[:n-2]
152+
} else {
153+
stk = append(stk, c)
154+
}
155+
}
156+
return len(stk) == 0
157+
}
75158
```
76159

77160
### **...**

solution/1000-1099/1003.Check If Word Is Valid After Substitutions/README_EN.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,84 @@ Thus, &quot;abcabcababcc&quot; is valid.
5757
### **Python3**
5858

5959
```python
60-
60+
class Solution:
61+
def isValid(self, s: str) -> bool:
62+
if len(s) % 3:
63+
return False
64+
stk = []
65+
for c in s:
66+
if c == 'c' and len(stk) > 1 and stk[-2] == 'a' and stk[-1] == 'b':
67+
stk = stk[:-2]
68+
else:
69+
stk.append(c)
70+
return not stk
6171
```
6272

6373
### **Java**
6474

6575
```java
76+
class Solution {
77+
public boolean isValid(String s) {
78+
if (s.length() % 3 > 0) {
79+
return false;
80+
}
81+
StringBuilder stk = new StringBuilder();
82+
for (char c : s.toCharArray()) {
83+
int n = stk.length();
84+
if (c == 'c' && n > 1 && stk.charAt(n - 2) == 'a' && stk.charAt(n - 1) == 'b') {
85+
stk.deleteCharAt(n - 1);
86+
stk.deleteCharAt(n - 2);
87+
} else {
88+
stk.append(c);
89+
}
90+
}
91+
return stk.length() == 0;
92+
}
93+
}
94+
```
95+
96+
### **C++**
97+
98+
```cpp
99+
class Solution {
100+
public:
101+
bool isValid(string s) {
102+
if (s.size() % 3) {
103+
return false;
104+
}
105+
string stk;
106+
for (char c : s) {
107+
int n = stk.size();
108+
if (c == 'c' && n > 1 && stk[n - 2] == 'a' && stk[n - 1] == 'b') {
109+
stk.pop_back();
110+
stk.pop_back();
111+
} else {
112+
stk.push_back(c);
113+
}
114+
}
115+
return stk.empty();
116+
}
117+
};
118+
```
66119
120+
### **Go**
121+
122+
```go
123+
func isValid(s string) bool {
124+
if len(s)%3 > 0 {
125+
return false
126+
}
127+
stk := []rune{}
128+
for _, c := range s {
129+
n := len(stk)
130+
if c == 'c' && n > 1 && stk[n-2] == 'a' && stk[n-1] == 'b' {
131+
stk = stk[:n-2]
132+
} else {
133+
stk = append(stk, c)
134+
}
135+
}
136+
return len(stk) == 0
137+
}
67138
```
68139

69140
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool isValid(string s) {
4+
if (s.size() % 3) {
5+
return false;
6+
}
7+
string stk;
8+
for (char c : s) {
9+
int n = stk.size();
10+
if (c == 'c' && n > 1 && stk[n - 2] == 'a' && stk[n - 1] == 'b') {
11+
stk.pop_back();
12+
stk.pop_back();
13+
} else {
14+
stk.push_back(c);
15+
}
16+
}
17+
return stk.empty();
18+
}
19+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func isValid(s string) bool {
2+
if len(s)%3 > 0 {
3+
return false
4+
}
5+
stk := []rune{}
6+
for _, c := range s {
7+
n := len(stk)
8+
if c == 'c' && n > 1 && stk[n-2] == 'a' && stk[n-1] == 'b' {
9+
stk = stk[:n-2]
10+
} else {
11+
stk = append(stk, c)
12+
}
13+
}
14+
return len(stk) == 0
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 isValid(String s) {
3+
if (s.length() % 3 > 0) {
4+
return false;
5+
}
6+
StringBuilder stk = new StringBuilder();
7+
for (char c : s.toCharArray()) {
8+
int n = stk.length();
9+
if (c == 'c' && n > 1 && stk.charAt(n - 2) == 'a' && stk.charAt(n - 1) == 'b') {
10+
stk.deleteCharAt(n - 1);
11+
stk.deleteCharAt(n - 2);
12+
} else {
13+
stk.append(c);
14+
}
15+
}
16+
return stk.length() == 0;
17+
}
18+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def isValid(self, s: str) -> bool:
3+
if len(s) % 3:
4+
return False
5+
stk = []
6+
for c in s:
7+
if c == 'c' and len(stk) > 1 and stk[-2] == 'a' and stk[-1] == 'b':
8+
stk = stk[:-2]
9+
else:
10+
stk.append(c)
11+
return not stk

0 commit comments

Comments
 (0)