Skip to content

Commit e9531d1

Browse files
authored
feat: add solutions to lc problems: No.3612,3613 (#4566)
* No.3612.Process String with Special Operations I * No.3613.Minimize Maximum Component Cost
1 parent 3c5a7b2 commit e9531d1

File tree

14 files changed

+739
-16
lines changed

14 files changed

+739
-16
lines changed

solution/3600-3699/3612.Process String with Special Operations I/README.md

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,32 +142,128 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3612.Pr
142142

143143
<!-- solution:start -->
144144

145-
### 方法一
145+
### 方法一:模拟
146+
147+
我们直接模拟题目中的操作即可。我们使用一个列表 $\text{result}$ 来存储当前的结果字符串。遍历输入字符串 $s$ 中的每个字符,根据字符的类型执行相应的操作:
148+
149+
- 如果字符是小写英文字母,则将其添加到 $\text{result}$ 中。
150+
- 如果字符是 `*`,则删除 $\text{result}$ 中的最后一个字符(如果存在)。
151+
- 如果字符是 `#`,则将 $\text{result}$ 复制一遍并追加到其自身后面。
152+
- 如果字符是 `%`,则反转 $\text{result}$。
153+
154+
最后,我们将 $\text{result}$ 转换为字符串并返回。
155+
156+
时间复杂度 $O(2^n)$,其中 $n$ 是字符串 $s$ 的长度。最坏情况下,可能会因为 `#` 操作导致 $\text{result}$ 的长度每次翻倍,因此时间复杂度是指数级的。忽略答案的空间消耗,空间复杂度 $O(1)$。
146157

147158
<!-- tabs:start -->
148159

149160
#### Python3
150161

151162
```python
152-
163+
class Solution:
164+
def processStr(self, s: str) -> str:
165+
result = []
166+
for c in s:
167+
if c.isalpha():
168+
result.append(c)
169+
elif c == "*" and result:
170+
result.pop()
171+
elif c == "#":
172+
result.extend(result)
173+
elif c == "%":
174+
result.reverse()
175+
return "".join(result)
153176
```
154177

155178
#### Java
156179

157180
```java
158-
181+
class Solution {
182+
public String processStr(String s) {
183+
StringBuilder result = new StringBuilder();
184+
for (char c : s.toCharArray()) {
185+
if (Character.isLetter(c)) {
186+
result.append(c);
187+
} else if (c == '*') {
188+
result.setLength(Math.max(0, result.length() - 1));
189+
} else if (c == '#') {
190+
result.append(result);
191+
} else if (c == '%') {
192+
result.reverse();
193+
}
194+
}
195+
return result.toString();
196+
}
197+
}
159198
```
160199

161200
#### C++
162201

163202
```cpp
164-
203+
class Solution {
204+
public:
205+
string processStr(string s) {
206+
string result;
207+
for (char c : s) {
208+
if (isalpha(c)) {
209+
result += c;
210+
} else if (c == '*') {
211+
if (!result.empty()) {
212+
result.pop_back();
213+
}
214+
} else if (c == '#') {
215+
result += result;
216+
} else if (c == '%') {
217+
ranges::reverse(result);
218+
}
219+
}
220+
return result;
221+
}
222+
};
165223
```
166224
167225
#### Go
168226
169227
```go
228+
func processStr(s string) string {
229+
var result []rune
230+
for _, c := range s {
231+
if unicode.IsLetter(c) {
232+
result = append(result, c)
233+
} else if c == '*' {
234+
if len(result) > 0 {
235+
result = result[:len(result)-1]
236+
}
237+
} else if c == '#' {
238+
result = append(result, result...)
239+
} else if c == '%' {
240+
slices.Reverse(result)
241+
}
242+
}
243+
return string(result)
244+
}
245+
```
170246

247+
#### TypeScript
248+
249+
```ts
250+
function processStr(s: string): string {
251+
const result: string[] = [];
252+
for (const c of s) {
253+
if (/[a-zA-Z]/.test(c)) {
254+
result.push(c);
255+
} else if (c === '*') {
256+
if (result.length > 0) {
257+
result.pop();
258+
}
259+
} else if (c === '#') {
260+
result.push(...result);
261+
} else if (c === '%') {
262+
result.reverse();
263+
}
264+
}
265+
return result.join('');
266+
}
171267
```
172268

173269
<!-- tabs:end -->

solution/3600-3699/3612.Process String with Special Operations I/README_EN.md

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,32 +140,128 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3612.Pr
140140

141141
<!-- solution:start -->
142142

143-
### Solution 1
143+
### Solution 1: Simulation
144+
145+
We can directly simulate the operations described in the problem. We use a list $\text{result}$ to store the current result string. For each character in the input string $s$, we perform the corresponding operation based on the character type:
146+
147+
- If the character is a lowercase English letter, add it to $\text{result}$.
148+
- If the character is `*`, delete the last character in $\text{result}$ (if it exists).
149+
- If the character is `#`, copy $\text{result}$ and append it to itself.
150+
- If the character is `%`, reverse $\text{result}$.
151+
152+
Finally, we convert $\text{result}$ to a string and return it.
153+
154+
The time complexity is $O(2^n)$, where $n$ is the length of string $s$. In the worst case, the `#` operation may cause the length of $\text{result}$ to double each time, resulting in exponential time complexity. Ignoring the space consumption of the answer, the space complexity is $O(1)$.
144155

145156
<!-- tabs:start -->
146157

147158
#### Python3
148159

149160
```python
150-
161+
class Solution:
162+
def processStr(self, s: str) -> str:
163+
result = []
164+
for c in s:
165+
if c.isalpha():
166+
result.append(c)
167+
elif c == "*" and result:
168+
result.pop()
169+
elif c == "#":
170+
result.extend(result)
171+
elif c == "%":
172+
result.reverse()
173+
return "".join(result)
151174
```
152175

153176
#### Java
154177

155178
```java
156-
179+
class Solution {
180+
public String processStr(String s) {
181+
StringBuilder result = new StringBuilder();
182+
for (char c : s.toCharArray()) {
183+
if (Character.isLetter(c)) {
184+
result.append(c);
185+
} else if (c == '*') {
186+
result.setLength(Math.max(0, result.length() - 1));
187+
} else if (c == '#') {
188+
result.append(result);
189+
} else if (c == '%') {
190+
result.reverse();
191+
}
192+
}
193+
return result.toString();
194+
}
195+
}
157196
```
158197

159198
#### C++
160199

161200
```cpp
162-
201+
class Solution {
202+
public:
203+
string processStr(string s) {
204+
string result;
205+
for (char c : s) {
206+
if (isalpha(c)) {
207+
result += c;
208+
} else if (c == '*') {
209+
if (!result.empty()) {
210+
result.pop_back();
211+
}
212+
} else if (c == '#') {
213+
result += result;
214+
} else if (c == '%') {
215+
ranges::reverse(result);
216+
}
217+
}
218+
return result;
219+
}
220+
};
163221
```
164222
165223
#### Go
166224
167225
```go
226+
func processStr(s string) string {
227+
var result []rune
228+
for _, c := range s {
229+
if unicode.IsLetter(c) {
230+
result = append(result, c)
231+
} else if c == '*' {
232+
if len(result) > 0 {
233+
result = result[:len(result)-1]
234+
}
235+
} else if c == '#' {
236+
result = append(result, result...)
237+
} else if c == '%' {
238+
slices.Reverse(result)
239+
}
240+
}
241+
return string(result)
242+
}
243+
```
168244

245+
#### TypeScript
246+
247+
```ts
248+
function processStr(s: string): string {
249+
const result: string[] = [];
250+
for (const c of s) {
251+
if (/[a-zA-Z]/.test(c)) {
252+
result.push(c);
253+
} else if (c === '*') {
254+
if (result.length > 0) {
255+
result.pop();
256+
}
257+
} else if (c === '#') {
258+
result.push(...result);
259+
} else if (c === '%') {
260+
result.reverse();
261+
}
262+
}
263+
return result.join('');
264+
}
169265
```
170266

171267
<!-- tabs:end -->
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
string processStr(string s) {
4+
string result;
5+
for (char c : s) {
6+
if (isalpha(c)) {
7+
result += c;
8+
} else if (c == '*') {
9+
if (!result.empty()) {
10+
result.pop_back();
11+
}
12+
} else if (c == '#') {
13+
result += result;
14+
} else if (c == '%') {
15+
ranges::reverse(result);
16+
}
17+
}
18+
return result;
19+
}
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func processStr(s string) string {
2+
var result []rune
3+
for _, c := range s {
4+
if unicode.IsLetter(c) {
5+
result = append(result, c)
6+
} else if c == '*' {
7+
if len(result) > 0 {
8+
result = result[:len(result)-1]
9+
}
10+
} else if c == '#' {
11+
result = append(result, result...)
12+
} else if c == '%' {
13+
slices.Reverse(result)
14+
}
15+
}
16+
return string(result)
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public String processStr(String s) {
3+
StringBuilder result = new StringBuilder();
4+
for (char c : s.toCharArray()) {
5+
if (Character.isLetter(c)) {
6+
result.append(c);
7+
} else if (c == '*') {
8+
result.setLength(Math.max(0, result.length() - 1));
9+
} else if (c == '#') {
10+
result.append(result);
11+
} else if (c == '%') {
12+
result.reverse();
13+
}
14+
}
15+
return result.toString();
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def processStr(self, s: str) -> str:
3+
result = []
4+
for c in s:
5+
if c.isalpha():
6+
result.append(c)
7+
elif c == "*" and result:
8+
result.pop()
9+
elif c == "#":
10+
result.extend(result)
11+
elif c == "%":
12+
result.reverse()
13+
return "".join(result)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function processStr(s: string): string {
2+
const result: string[] = [];
3+
for (const c of s) {
4+
if (/[a-zA-Z]/.test(c)) {
5+
result.push(c);
6+
} else if (c === '*') {
7+
if (result.length > 0) {
8+
result.pop();
9+
}
10+
} else if (c === '#') {
11+
result.push(...result);
12+
} else if (c === '%') {
13+
result.reverse();
14+
}
15+
}
16+
return result.join('');
17+
}

0 commit comments

Comments
 (0)