Skip to content

Commit 81b2e09

Browse files
committed
feat: add solutions to lc problem: No.1003
No.1003.Check If Word Is Valid After Substitutions
1 parent 0f0f282 commit 81b2e09

File tree

7 files changed

+127
-97
lines changed

7 files changed

+127
-97
lines changed

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

+44-37
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,15 @@
5656

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

59-
**方法一:栈模拟**
59+
**方法一:**
6060

61-
先判断字符串长度是否为 $3$ 的倍数,若不是直接返回 `false`
61+
我们观察题目中的操作,可以发现,每一次都会在字符串的任意位置插入字符串 `"abc"`,所以每次插入操作之后,字符串的长度都会增加 $3$。如果字符串 $s$ 有效,那么它的长度一定是 $3$ 的倍数。因此,我们先对字符串 $s$ 的长度进行判断,如果不是 $3$ 的倍数,那么 $s$ 一定无效,可以直接返回 `false`
6262

63-
接下来我们使用栈模拟操作,遍历字符串 $s$ 的每个字符 $c$
63+
接下来我们遍历字符串 $s$ 的每个字符 $c$,我们先将字符 $c$ 压入栈 $t$ 中。如果此时栈 $t$ 的长度大于等于 $3$,并且栈顶的三个元素组成了字符串 `"abc"`,那么我们就将栈顶的三个元素弹出。然后继续遍历字符串 $s$ 的下一个字符。
6464

65-
若 $c$ 等于 `'c'`,且栈顶的两个元素分别为 `'a'``'b'`,则将栈顶的两个元素出栈;否则将 $c$ 入栈
65+
遍历结束之后,如果栈 $t$ 为空,那么说明字符串 $s$ 有效,返回 `true`,否则返回 `false`
6666

67-
最后判断栈是否为空,若为空则返回 `true`,否则返回 `false`
68-
69-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
7068

7169
<!-- tabs:start -->
7270

@@ -79,13 +77,12 @@ class Solution:
7977
def isValid(self, s: str) -> bool:
8078
if len(s) % 3:
8179
return False
82-
stk = []
80+
t = []
8381
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
82+
t.append(c)
83+
if ''.join(t[-3:]) == 'abc':
84+
t[-3:] = []
85+
return not t
8986
```
9087

9188
### **Java**
@@ -98,17 +95,14 @@ class Solution {
9895
if (s.length() % 3 > 0) {
9996
return false;
10097
}
101-
StringBuilder stk = new StringBuilder();
98+
StringBuilder t = new StringBuilder();
10299
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);
100+
t.append(c);
101+
if (t.length() >= 3 && "abc".equals(t.substring(t.length() - 3))) {
102+
t.delete(t.length() - 3, t.length());
109103
}
110104
}
111-
return stk.length() == 0;
105+
return t.isEmpty();
112106
}
113107
}
114108
```
@@ -122,17 +116,14 @@ public:
122116
if (s.size() % 3) {
123117
return false;
124118
}
125-
string stk;
119+
string t;
126120
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);
121+
t.push_back(c);
122+
if (t.size() >= 3 && t.substr(t.size() - 3, 3) == "abc") {
123+
t.erase(t.end() - 3, t.end());
133124
}
134125
}
135-
return stk.empty();
126+
return t.empty();
136127
}
137128
};
138129
```
@@ -144,16 +135,32 @@ func isValid(s string) bool {
144135
if len(s)%3 > 0 {
145136
return false
146137
}
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)
138+
t := []byte{}
139+
for i := range s {
140+
t = append(t, s[i])
141+
if len(t) >= 3 && string(t[len(t)-3:]) == "abc" {
142+
t = t[:len(t)-3]
154143
}
155144
}
156-
return len(stk) == 0
145+
return len(t) == 0
146+
}
147+
```
148+
149+
### **TypeScript**
150+
151+
```ts
152+
function isValid(s: string): boolean {
153+
if (s.length % 3 !== 0) {
154+
return false;
155+
}
156+
const t: string[] = [];
157+
for (const c of s) {
158+
t.push(c);
159+
if (t.slice(-3).join('') === 'abc') {
160+
t.splice(-3);
161+
}
162+
}
163+
return t.length === 0;
157164
}
158165
```
159166

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

+49-30
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ Thus, &quot;abcabcababcc&quot; is valid.
5252

5353
## Solutions
5454

55+
**Solution 1: Stack**
56+
57+
If the string is valid, it's length must be the multiple of $3$.
58+
59+
We traverse the string and push every character into the stack $t$. If the size of stack $t$ is greater than or equal to $3$ and the top three elements of stack $t$ constitute the string `"abc"`, we pop the top three elements. Then we continue to traverse the next character of the string $s$.
60+
61+
When the traversal is over, if the stack $t$ is empty, the string $s$ is valid, return `true`, otherwise return `false`.
62+
63+
The time complexity is $O(n)$ and the space complexity is $O(n)$. Where $n$ is the length of the string $s$.
64+
5565
<!-- tabs:start -->
5666

5767
### **Python3**
@@ -61,13 +71,12 @@ class Solution:
6171
def isValid(self, s: str) -> bool:
6272
if len(s) % 3:
6373
return False
64-
stk = []
74+
t = []
6575
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
76+
t.append(c)
77+
if ''.join(t[-3:]) == 'abc':
78+
t[-3:] = []
79+
return not t
7180
```
7281

7382
### **Java**
@@ -78,17 +87,14 @@ class Solution {
7887
if (s.length() % 3 > 0) {
7988
return false;
8089
}
81-
StringBuilder stk = new StringBuilder();
90+
StringBuilder t = new StringBuilder();
8291
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);
92+
t.append(c);
93+
if (t.length() >= 3 && "abc".equals(t.substring(t.length() - 3))) {
94+
t.delete(t.length() - 3, t.length());
8995
}
9096
}
91-
return stk.length() == 0;
97+
return t.isEmpty();
9298
}
9399
}
94100
```
@@ -102,17 +108,14 @@ public:
102108
if (s.size() % 3) {
103109
return false;
104110
}
105-
string stk;
111+
string t;
106112
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+
t.push_back(c);
114+
if (t.size() >= 3 && t.substr(t.size() - 3, 3) == "abc") {
115+
t.erase(t.end() - 3, t.end());
113116
}
114117
}
115-
return stk.empty();
118+
return t.empty();
116119
}
117120
};
118121
```
@@ -124,16 +127,32 @@ func isValid(s string) bool {
124127
if len(s)%3 > 0 {
125128
return false
126129
}
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)
130+
t := []byte{}
131+
for i := range s {
132+
t = append(t, s[i])
133+
if len(t) >= 3 && string(t[len(t)-3:]) == "abc" {
134+
t = t[:len(t)-3]
134135
}
135136
}
136-
return len(stk) == 0
137+
return len(t) == 0
138+
}
139+
```
140+
141+
### **TypeScript**
142+
143+
```ts
144+
function isValid(s: string): boolean {
145+
if (s.length % 3 !== 0) {
146+
return false;
147+
}
148+
const t: string[] = [];
149+
for (const c of s) {
150+
t.push(c);
151+
if (t.slice(-3).join('') === 'abc') {
152+
t.splice(-3);
153+
}
154+
}
155+
return t.length === 0;
137156
}
138157
```
139158

solution/1000-1099/1003.Check If Word Is Valid After Substitutions/Solution.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@ class Solution {
44
if (s.size() % 3) {
55
return false;
66
}
7-
string stk;
7+
string t;
88
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);
9+
t.push_back(c);
10+
if (t.size() >= 3 && t.substr(t.size() - 3, 3) == "abc") {
11+
t.erase(t.end() - 3, t.end());
1512
}
1613
}
17-
return stk.empty();
14+
return t.empty();
1815
}
1916
};

solution/1000-1099/1003.Check If Word Is Valid After Substitutions/Solution.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ func isValid(s string) bool {
22
if len(s)%3 > 0 {
33
return false
44
}
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)
5+
t := []byte{}
6+
for i := range s {
7+
t = append(t, s[i])
8+
if len(t) >= 3 && string(t[len(t)-3:]) == "abc" {
9+
t = t[:len(t)-3]
1210
}
1311
}
14-
return len(stk) == 0
12+
return len(t) == 0
1513
}

solution/1000-1099/1003.Check If Word Is Valid After Substitutions/Solution.java

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@ public boolean isValid(String s) {
33
if (s.length() % 3 > 0) {
44
return false;
55
}
6-
StringBuilder stk = new StringBuilder();
6+
StringBuilder t = new StringBuilder();
77
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);
8+
t.append(c);
9+
if (t.length() >= 3 && "abc".equals(t.substring(t.length() - 3))) {
10+
t.delete(t.length() - 3, t.length());
1411
}
1512
}
16-
return stk.length() == 0;
13+
return t.isEmpty();
1714
}
1815
}

solution/1000-1099/1003.Check If Word Is Valid After Substitutions/Solution.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ class Solution:
22
def isValid(self, s: str) -> bool:
33
if len(s) % 3:
44
return False
5-
stk = []
5+
t = []
66
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
7+
t.append(c)
8+
if ''.join(t[-3:]) == 'abc':
9+
t[-3:] = []
10+
return not t
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function isValid(s: string): boolean {
2+
if (s.length % 3 !== 0) {
3+
return false;
4+
}
5+
const t: string[] = [];
6+
for (const c of s) {
7+
t.push(c);
8+
if (t.slice(-3).join('') === 'abc') {
9+
t.splice(-3);
10+
}
11+
}
12+
return t.length === 0;
13+
}

0 commit comments

Comments
 (0)