Skip to content

Commit cc82696

Browse files
committed
feat: add solutions to lc problem: No.0423
No.0423.Reconstruct Original Digits from English
1 parent e56bf5f commit cc82696

File tree

6 files changed

+330
-96
lines changed

6 files changed

+330
-96
lines changed

solution/0400-0499/0423.Reconstruct Original Digits from English/README.md

+129-1
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,155 @@
3232
输出: "45" (fourfive)
3333
</pre>
3434

35-
3635
## 解法
3736

3837
<!-- 这里可写通用的实现逻辑 -->
3938

39+
统计 `["e","g","f","i","h","o","n","s","r","u","t","w","v","x","z"]` 每个字母在哪些数字出现过。
40+
41+
| 字母 | 数字 |
42+
| ---- | ------------- |
43+
| e | 0 1 3 5 7 8 9 |
44+
| g | 8 |
45+
| f | 4 5 |
46+
| i | 5 6 8 9 |
47+
| h | 3 8 |
48+
| o | 0 1 2 4 |
49+
| n | 1 7 9 |
50+
| s | 6 7 |
51+
| r | 0 3 4 |
52+
| u | 4 |
53+
| t | 2 3 8 |
54+
| w | 2 |
55+
| v | 5 7 |
56+
| x | 6 |
57+
| z | 0 |
58+
59+
由于部分字母只在某个数字出现过,比如字母 `z` 只在 `0` 出现过,因此我们统计英文中 `z` 的数量,就可以推断数字 0 的个数,依次类推。
60+
4061
<!-- tabs:start -->
4162

4263
### **Python3**
4364

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

4667
```python
68+
class Solution:
69+
def originalDigits(self, s: str) -> str:
70+
counter = Counter(s)
71+
cnt = [0] * 10
72+
73+
cnt[0] = counter['z']
74+
cnt[2] = counter['w']
75+
cnt[4] = counter['u']
76+
cnt[6] = counter['x']
77+
cnt[8] = counter['g']
78+
79+
cnt[3] = counter['h'] - cnt[8]
80+
cnt[5] = counter['f'] - cnt[4]
81+
cnt[7] = counter['s'] - cnt[6]
4782

83+
cnt[1] = counter['o'] - cnt[0] - cnt[2] - cnt[4]
84+
cnt[9] = counter['i'] - cnt[5] - cnt[6] - cnt[8]
85+
86+
return ''.join(cnt[i] * str(i) for i in range(10))
4887
```
4988

5089
### **Java**
5190

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

5493
```java
94+
class Solution {
95+
public String originalDigits(String s) {
96+
int[] counter = new int[26];
97+
for (char c : s.toCharArray()) {
98+
++counter[c - 'a'];
99+
}
100+
int[] cnt = new int[10];
101+
cnt[0] = counter['z' - 'a'];
102+
cnt[2] = counter['w' - 'a'];
103+
cnt[4] = counter['u' - 'a'];
104+
cnt[6] = counter['x' - 'a'];
105+
cnt[8] = counter['g' - 'a'];
106+
107+
cnt[3] = counter['h' - 'a'] - cnt[8];
108+
cnt[5] = counter['f' - 'a'] - cnt[4];
109+
cnt[7] = counter['s' - 'a'] - cnt[6];
110+
111+
cnt[1] = counter['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];
112+
cnt[9] = counter['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];
113+
114+
StringBuilder sb = new StringBuilder();
115+
for (int i = 0; i < 10; ++i) {
116+
for (int j = 0; j < cnt[i]; ++j) {
117+
sb.append(i);
118+
}
119+
}
120+
return sb.toString();
121+
}
122+
}
123+
```
124+
125+
### **C++**
126+
127+
```cpp
128+
class Solution {
129+
public:
130+
string originalDigits(string s) {
131+
vector<int> counter(26);
132+
for (char c : s) ++counter[c - 'a'];
133+
vector<int> cnt(10);
134+
cnt[0] = counter['z' - 'a'];
135+
cnt[2] = counter['w' - 'a'];
136+
cnt[4] = counter['u' - 'a'];
137+
cnt[6] = counter['x' - 'a'];
138+
cnt[8] = counter['g' - 'a'];
139+
140+
cnt[3] = counter['h' - 'a'] - cnt[8];
141+
cnt[5] = counter['f' - 'a'] - cnt[4];
142+
cnt[7] = counter['s' - 'a'] - cnt[6];
143+
144+
cnt[1] = counter['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];
145+
cnt[9] = counter['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];
146+
147+
string ans;
148+
for (int i = 0; i < 10; ++i)
149+
for (int j = 0; j < cnt[i]; ++j)
150+
ans += char(i + '0');
151+
return ans;
152+
}
153+
};
154+
```
55155

156+
### **Go**
157+
158+
```go
159+
func originalDigits(s string) string {
160+
counter := make([]int, 26)
161+
for _, c := range s {
162+
counter[c-'a']++
163+
}
164+
cnt := make([]int, 10)
165+
cnt[0] = counter['z'-'a']
166+
cnt[2] = counter['w'-'a']
167+
cnt[4] = counter['u'-'a']
168+
cnt[6] = counter['x'-'a']
169+
cnt[8] = counter['g'-'a']
170+
171+
cnt[3] = counter['h'-'a'] - cnt[8]
172+
cnt[5] = counter['f'-'a'] - cnt[4]
173+
cnt[7] = counter['s'-'a'] - cnt[6]
174+
175+
cnt[1] = counter['o'-'a'] - cnt[0] - cnt[2] - cnt[4]
176+
cnt[9] = counter['i'-'a'] - cnt[5] - cnt[6] - cnt[8]
177+
178+
ans := []byte{}
179+
for i, c := range cnt {
180+
ans = append(ans, bytes.Repeat([]byte{byte('0' + i)}, c)...)
181+
}
182+
return string(ans)
183+
}
56184
```
57185

58186
### **...**

solution/0400-0499/0423.Reconstruct Original Digits from English/README_EN.md

+107-1
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,127 @@
2323
<li><code>s</code> is <strong>guaranteed</strong> to be valid.</li>
2424
</ul>
2525

26-
2726
## Solutions
2827

2928
<!-- tabs:start -->
3029

3130
### **Python3**
3231

3332
```python
33+
class Solution:
34+
def originalDigits(self, s: str) -> str:
35+
counter = Counter(s)
36+
cnt = [0] * 10
37+
38+
cnt[0] = counter['z']
39+
cnt[2] = counter['w']
40+
cnt[4] = counter['u']
41+
cnt[6] = counter['x']
42+
cnt[8] = counter['g']
43+
44+
cnt[3] = counter['h'] - cnt[8]
45+
cnt[5] = counter['f'] - cnt[4]
46+
cnt[7] = counter['s'] - cnt[6]
47+
48+
cnt[1] = counter['o'] - cnt[0] - cnt[2] - cnt[4]
49+
cnt[9] = counter['i'] - cnt[5] - cnt[6] - cnt[8]
3450

51+
return ''.join(cnt[i] * str(i) for i in range(10))
3552
```
3653

3754
### **Java**
3855

3956
```java
57+
class Solution {
58+
public String originalDigits(String s) {
59+
int[] counter = new int[26];
60+
for (char c : s.toCharArray()) {
61+
++counter[c - 'a'];
62+
}
63+
int[] cnt = new int[10];
64+
cnt[0] = counter['z' - 'a'];
65+
cnt[2] = counter['w' - 'a'];
66+
cnt[4] = counter['u' - 'a'];
67+
cnt[6] = counter['x' - 'a'];
68+
cnt[8] = counter['g' - 'a'];
69+
70+
cnt[3] = counter['h' - 'a'] - cnt[8];
71+
cnt[5] = counter['f' - 'a'] - cnt[4];
72+
cnt[7] = counter['s' - 'a'] - cnt[6];
73+
74+
cnt[1] = counter['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];
75+
cnt[9] = counter['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];
76+
77+
StringBuilder sb = new StringBuilder();
78+
for (int i = 0; i < 10; ++i) {
79+
for (int j = 0; j < cnt[i]; ++j) {
80+
sb.append(i);
81+
}
82+
}
83+
return sb.toString();
84+
}
85+
}
86+
```
87+
88+
### **C++**
89+
90+
```cpp
91+
class Solution {
92+
public:
93+
string originalDigits(string s) {
94+
vector<int> counter(26);
95+
for (char c : s) ++counter[c - 'a'];
96+
vector<int> cnt(10);
97+
cnt[0] = counter['z' - 'a'];
98+
cnt[2] = counter['w' - 'a'];
99+
cnt[4] = counter['u' - 'a'];
100+
cnt[6] = counter['x' - 'a'];
101+
cnt[8] = counter['g' - 'a'];
102+
103+
cnt[3] = counter['h' - 'a'] - cnt[8];
104+
cnt[5] = counter['f' - 'a'] - cnt[4];
105+
cnt[7] = counter['s' - 'a'] - cnt[6];
106+
107+
cnt[1] = counter['o' - 'a'] - cnt[0] - cnt[2] - cnt[4];
108+
cnt[9] = counter['i' - 'a'] - cnt[5] - cnt[6] - cnt[8];
109+
110+
string ans;
111+
for (int i = 0; i < 10; ++i)
112+
for (int j = 0; j < cnt[i]; ++j)
113+
ans += char(i + '0');
114+
return ans;
115+
}
116+
};
117+
```
40118

119+
### **Go**
120+
121+
```go
122+
func originalDigits(s string) string {
123+
counter := make([]int, 26)
124+
for _, c := range s {
125+
counter[c-'a']++
126+
}
127+
cnt := make([]int, 10)
128+
cnt[0] = counter['z'-'a']
129+
cnt[2] = counter['w'-'a']
130+
cnt[4] = counter['u'-'a']
131+
cnt[6] = counter['x'-'a']
132+
cnt[8] = counter['g'-'a']
133+
134+
cnt[3] = counter['h'-'a'] - cnt[8]
135+
cnt[5] = counter['f'-'a'] - cnt[4]
136+
cnt[7] = counter['s'-'a'] - cnt[6]
137+
138+
cnt[1] = counter['o'-'a'] - cnt[0] - cnt[2] - cnt[4]
139+
cnt[9] = counter['i'-'a'] - cnt[5] - cnt[6] - cnt[8]
140+
141+
ans := []byte{}
142+
for i, c := range cnt {
143+
ans = append(ans, bytes.Repeat([]byte{byte('0' + i)}, c)...)
144+
}
145+
return string(ans)
146+
}
41147
```
42148

43149
### **...**

0 commit comments

Comments
 (0)