Skip to content

Commit 87c04c5

Browse files
committed
feat: add solutions to lc problem: No.1417
No.1417.Reformat The String
1 parent 5b7be54 commit 87c04c5

File tree

6 files changed

+328
-2
lines changed

6 files changed

+328
-2
lines changed

solution/1400-1499/1417.Reformat The String/README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,140 @@
6060

6161
<!-- 这里可写通用的实现逻辑 -->
6262

63+
**方法一:模拟**
64+
65+
将字符串 $s$ 中的所有字符分成“数字”、“字母”两类,分别放入 $a$, $b$ 两个数组中。
66+
67+
比较 $a$, $b$ 两个数组的长度,若 $a$ 长度小于 $b$,则交换 $a$, $b$。接着判断两个数组长度差,若超过 $1$,则返回空字符串。
68+
69+
接着同时遍历两个数组,依次添加 $a$, $b$ 中对应字符到答案中。遍历结束,若 $a$ 长度大于 $b$,则添加 $a$ 的最后一个字符到答案中。
70+
71+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是字符串 $s$ 的长度。
72+
6373
<!-- tabs:start -->
6474

6575
### **Python3**
6676

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

6979
```python
70-
80+
class Solution:
81+
def reformat(self, s: str) -> str:
82+
a = [c for c in s if c.islower()]
83+
b = [c for c in s if c.isdigit()]
84+
if abs(len(a) - len(b)) > 1:
85+
return ''
86+
if len(a) < len(b):
87+
a, b = b, a
88+
ans = []
89+
for x, y in zip(a, b):
90+
ans.append(x + y)
91+
if len(a) > len(b):
92+
ans.append(a[-1])
93+
return ''.join(ans)
7194
```
7295

7396
### **Java**
7497

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

77100
```java
101+
class Solution {
102+
public String reformat(String s) {
103+
StringBuilder a = new StringBuilder();
104+
StringBuilder b = new StringBuilder();
105+
for (char c : s.toCharArray()) {
106+
if (Character.isDigit(c)) {
107+
a.append(c);
108+
} else {
109+
b.append(c);
110+
}
111+
}
112+
int m = a.length(), n = b.length();
113+
if (Math.abs(m - n) > 1) {
114+
return "";
115+
}
116+
StringBuilder ans = new StringBuilder();
117+
for (int i = 0; i < Math.min(m, n); ++i) {
118+
if (m > n) {
119+
ans.append(a.charAt(i));
120+
ans.append(b.charAt(i));
121+
} else {
122+
ans.append(b.charAt(i));
123+
ans.append(a.charAt(i));
124+
}
125+
126+
}
127+
if (m > n) {
128+
ans.append(a.charAt(m - 1));
129+
}
130+
if (m < n) {
131+
ans.append(b.charAt(n - 1));
132+
}
133+
return ans.toString();
134+
}
135+
}
136+
```
137+
138+
### **C++**
139+
140+
```cpp
141+
class Solution {
142+
public:
143+
string reformat(string s) {
144+
string a = "", b = "";
145+
for (char c : s) {
146+
if (isdigit(c)) a += c;
147+
else b += c;
148+
}
149+
int m = a.size(), n = b.size();
150+
if (abs(m - n) > 1) return "";
151+
string ans = "";
152+
for (int i = 0; i < min(m, n); ++i) {
153+
if (m > n) {
154+
ans += a[i];
155+
ans += b[i];
156+
} else {
157+
ans += b[i];
158+
ans += a[i];
159+
}
160+
}
161+
if (m > n) ans += a[m - 1];
162+
if (m < n) ans += b[n - 1];
163+
return ans;
164+
}
165+
};
166+
```
78167
168+
### **Go**
169+
170+
```go
171+
func reformat(s string) string {
172+
a := []byte{}
173+
b := []byte{}
174+
for _, c := range s {
175+
if unicode.IsLetter(c) {
176+
a = append(a, byte(c))
177+
} else {
178+
b = append(b, byte(c))
179+
}
180+
}
181+
if len(a) < len(b) {
182+
a, b = b, a
183+
}
184+
if len(a)-len(b) > 1 {
185+
return ""
186+
}
187+
var ans strings.Builder
188+
for i := range b {
189+
ans.WriteByte(a[i])
190+
ans.WriteByte(b[i])
191+
}
192+
if len(a) > len(b) {
193+
ans.WriteByte(a[len(a)-1])
194+
}
195+
return ans.String()
196+
}
79197
```
80198

81199
### **...**

solution/1400-1499/1417.Reformat The String/README_EN.md

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,121 @@
5050
### **Python3**
5151

5252
```python
53-
53+
class Solution:
54+
def reformat(self, s: str) -> str:
55+
a = [c for c in s if c.islower()]
56+
b = [c for c in s if c.isdigit()]
57+
if abs(len(a) - len(b)) > 1:
58+
return ''
59+
if len(a) < len(b):
60+
a, b = b, a
61+
ans = []
62+
for x, y in zip(a, b):
63+
ans.append(x + y)
64+
if len(a) > len(b):
65+
ans.append(a[-1])
66+
return ''.join(ans)
5467
```
5568

5669
### **Java**
5770

5871
```java
72+
class Solution {
73+
public String reformat(String s) {
74+
StringBuilder a = new StringBuilder();
75+
StringBuilder b = new StringBuilder();
76+
for (char c : s.toCharArray()) {
77+
if (Character.isDigit(c)) {
78+
a.append(c);
79+
} else {
80+
b.append(c);
81+
}
82+
}
83+
int m = a.length(), n = b.length();
84+
if (Math.abs(m - n) > 1) {
85+
return "";
86+
}
87+
StringBuilder ans = new StringBuilder();
88+
for (int i = 0; i < Math.min(m, n); ++i) {
89+
if (m > n) {
90+
ans.append(a.charAt(i));
91+
ans.append(b.charAt(i));
92+
} else {
93+
ans.append(b.charAt(i));
94+
ans.append(a.charAt(i));
95+
}
96+
97+
}
98+
if (m > n) {
99+
ans.append(a.charAt(m - 1));
100+
}
101+
if (m < n) {
102+
ans.append(b.charAt(n - 1));
103+
}
104+
return ans.toString();
105+
}
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
string reformat(string s) {
115+
string a = "", b = "";
116+
for (char c : s) {
117+
if (isdigit(c)) a += c;
118+
else b += c;
119+
}
120+
int m = a.size(), n = b.size();
121+
if (abs(m - n) > 1) return "";
122+
string ans = "";
123+
for (int i = 0; i < min(m, n); ++i) {
124+
if (m > n) {
125+
ans += a[i];
126+
ans += b[i];
127+
} else {
128+
ans += b[i];
129+
ans += a[i];
130+
}
131+
}
132+
if (m > n) ans += a[m - 1];
133+
if (m < n) ans += b[n - 1];
134+
return ans;
135+
}
136+
};
137+
```
59138
139+
### **Go**
140+
141+
```go
142+
func reformat(s string) string {
143+
a := []byte{}
144+
b := []byte{}
145+
for _, c := range s {
146+
if unicode.IsLetter(c) {
147+
a = append(a, byte(c))
148+
} else {
149+
b = append(b, byte(c))
150+
}
151+
}
152+
if len(a) < len(b) {
153+
a, b = b, a
154+
}
155+
if len(a)-len(b) > 1 {
156+
return ""
157+
}
158+
var ans strings.Builder
159+
for i := range b {
160+
ans.WriteByte(a[i])
161+
ans.WriteByte(b[i])
162+
}
163+
if len(a) > len(b) {
164+
ans.WriteByte(a[len(a)-1])
165+
}
166+
return ans.String()
167+
}
60168
```
61169

62170
### **...**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string reformat(string s) {
4+
string a = "", b = "";
5+
for (char c : s) {
6+
if (isdigit(c)) a += c;
7+
else b += c;
8+
}
9+
int m = a.size(), n = b.size();
10+
if (abs(m - n) > 1) return "";
11+
string ans = "";
12+
for (int i = 0; i < min(m, n); ++i) {
13+
if (m > n) {
14+
ans += a[i];
15+
ans += b[i];
16+
} else {
17+
ans += b[i];
18+
ans += a[i];
19+
}
20+
}
21+
if (m > n) ans += a[m - 1];
22+
if (m < n) ans += b[n - 1];
23+
return ans;
24+
}
25+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
func reformat(s string) string {
2+
a := []byte{}
3+
b := []byte{}
4+
for _, c := range s {
5+
if unicode.IsLetter(c) {
6+
a = append(a, byte(c))
7+
} else {
8+
b = append(b, byte(c))
9+
}
10+
}
11+
if len(a) < len(b) {
12+
a, b = b, a
13+
}
14+
if len(a)-len(b) > 1 {
15+
return ""
16+
}
17+
var ans strings.Builder
18+
for i := range b {
19+
ans.WriteByte(a[i])
20+
ans.WriteByte(b[i])
21+
}
22+
if len(a) > len(b) {
23+
ans.WriteByte(a[len(a)-1])
24+
}
25+
return ans.String()
26+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public String reformat(String s) {
3+
StringBuilder a = new StringBuilder();
4+
StringBuilder b = new StringBuilder();
5+
for (char c : s.toCharArray()) {
6+
if (Character.isDigit(c)) {
7+
a.append(c);
8+
} else {
9+
b.append(c);
10+
}
11+
}
12+
int m = a.length(), n = b.length();
13+
if (Math.abs(m - n) > 1) {
14+
return "";
15+
}
16+
StringBuilder ans = new StringBuilder();
17+
for (int i = 0; i < Math.min(m, n); ++i) {
18+
if (m > n) {
19+
ans.append(a.charAt(i));
20+
ans.append(b.charAt(i));
21+
} else {
22+
ans.append(b.charAt(i));
23+
ans.append(a.charAt(i));
24+
}
25+
26+
}
27+
if (m > n) {
28+
ans.append(a.charAt(m - 1));
29+
}
30+
if (m < n) {
31+
ans.append(b.charAt(n - 1));
32+
}
33+
return ans.toString();
34+
}
35+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def reformat(self, s: str) -> str:
3+
a = [c for c in s if c.islower()]
4+
b = [c for c in s if c.isdigit()]
5+
if abs(len(a) - len(b)) > 1:
6+
return ''
7+
if len(a) < len(b):
8+
a, b = b, a
9+
ans = []
10+
for x, y in zip(a, b):
11+
ans.append(x + y)
12+
if len(a) > len(b):
13+
ans.append(a[-1])
14+
return ''.join(ans)

0 commit comments

Comments
 (0)