Skip to content

Commit 0168c94

Browse files
committed
feat: add solutions to lc problems: No.2451~2454
* No.2451.Odd String Difference * No.2452.Words Within Two Edits of Dictionary * No.2453.Destroy Sequential Targets * No.2454.Next Greater Element IV
1 parent e77a392 commit 0168c94

File tree

26 files changed

+915
-18
lines changed

26 files changed

+915
-18
lines changed

solution/0700-0799/0784.Letter Case Permutation/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151

5252
**方法二:二进制枚举**
5353

54-
对于一个字母,我们可以将其转换为大写或小写,因此对于每个字母,我们可以使用一个二进制位表示其转换的方案,即 $0$ 表示大写,而 $1$ 表示小写
54+
对于一个字母,我们可以将其转换为大写或小写,因此对于每个字母,我们可以使用一个二进制位表示其转换的方案,其中 $1$ 表示小写,而 $0$ 表示大写
5555

5656
我们先统计字符串 $s$ 中字母的个数,记为 $n$,那么一共有 $2^n$ 种转换方案,我们可以使用二进制数的每一位表示每个字母的转换方案,从 $0$ 到 $2^n-1$ 进行枚举。
5757

58-
具体地,我们可以使用一个变量 $i$ 表示当前枚举到的二进制数,其中 $i$ 的第 $j$ 位表示第 $j$ 个字母的转换方案。即 $i$ 的第 $j$ 位为 $0$ 表示第 $j$ 个字母转换为大写,而 $i$ 的第 $j$ 位为 $1$ 表示第 $j$ 个字母转换为小写
58+
具体地,我们可以使用一个变量 $i$ 表示当前枚举到的二进制数,其中 $i$ 的第 $j$ 位表示第 $j$ 个字母的转换方案。即 $i$ 的第 $j$ 位为 $1$ 表示第 $j$ 个字母转换为小写,而 $i$ 的第 $j$ 位为 $0$ 表示第 $j$ 个字母转换为大写
5959

6060
时间复杂度 $O(n\times 2^n)$,其中 $n$ 是字符串 $s$ 的长度。对于每个字母,我们可以选择将其转换为大写或小写,因此一共有 $2^n$ 种转换方案。对于每种转换方案,我们需要 $O(n)$ 的时间生成一个新的字符串。
6161

@@ -144,7 +144,7 @@ class Solution {
144144
for (int k = 0; k < s.length(); ++k) {
145145
char x = s.charAt(k);
146146
if (x >= 'A') {
147-
x = ((i >> j) & 1) == 1 ? Character.toUpperCase(x) : Character.toLowerCase(x);
147+
x = ((i >> j) & 1) == 1 ? Character.toLowerCase(x) : Character.toUpperCase(x);
148148
++j;
149149
}
150150
t.append(x);
@@ -192,7 +192,7 @@ public:
192192
string t;
193193
for (char c : s) {
194194
if (isalpha(c)) {
195-
c = (i >> j & 1) ? toupper(c) : tolower(c);
195+
c = (i >> j & 1) ? tolower(c) : toupper(c);
196196
++j;
197197
}
198198
t += c;
@@ -241,9 +241,9 @@ func letterCasePermutation(s string) (ans []string) {
241241
for _, c := range s {
242242
if c >= 'A' {
243243
if ((i >> j) & 1) == 1 {
244-
c = unicode.ToUpper(c)
245-
} else {
246244
c = unicode.ToLower(c)
245+
} else {
246+
c = unicode.ToUpper(c)
247247
}
248248
j++
249249
}

solution/0700-0799/0784.Letter Case Permutation/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Solution {
116116
for (int k = 0; k < s.length(); ++k) {
117117
char x = s.charAt(k);
118118
if (x >= 'A') {
119-
x = ((i >> j) & 1) == 1 ? Character.toUpperCase(x) : Character.toLowerCase(x);
119+
x = ((i >> j) & 1) == 1 ? Character.toLowerCase(x) : Character.toUpperCase(x);
120120
++j;
121121
}
122122
t.append(x);
@@ -164,7 +164,7 @@ public:
164164
string t;
165165
for (char c : s) {
166166
if (isalpha(c)) {
167-
c = (i >> j & 1) ? toupper(c) : tolower(c);
167+
c = (i >> j & 1) ? tolower(c) : toupper(c);
168168
++j;
169169
}
170170
t += c;
@@ -213,9 +213,9 @@ func letterCasePermutation(s string) (ans []string) {
213213
for _, c := range s {
214214
if c >= 'A' {
215215
if ((i >> j) & 1) == 1 {
216-
c = unicode.ToUpper(c)
217-
} else {
218216
c = unicode.ToLower(c)
217+
} else {
218+
c = unicode.ToUpper(c)
219219
}
220220
j++
221221
}

solution/2400-2499/2451.Odd String Difference/README.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,85 @@
6262
<!-- 这里可写当前语言的特殊实现逻辑 -->
6363

6464
```python
65-
65+
class Solution:
66+
def oddString(self, words: List[str]) -> str:
67+
cnt = defaultdict(list)
68+
for w in words:
69+
d = [str(ord(b) - ord(a)) for a, b in pairwise(w)]
70+
cnt[','.join(d)].append(w)
71+
return next(v[0] for v in cnt.values() if len(v) == 1)
6672
```
6773

6874
### **Java**
6975

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

7278
```java
79+
class Solution {
80+
public String oddString(String[] words) {
81+
Map<String, List<String>> cnt = new HashMap<>();
82+
for (var w : words) {
83+
List<String> d = new ArrayList<>();
84+
for (int i = 0; i < w.length() - 1; ++i) {
85+
d.add(String.valueOf(w.charAt(i + 1) - w.charAt(i)));
86+
}
87+
cnt.computeIfAbsent(String.join(",", d), k -> new ArrayList<>()).add(w);
88+
}
89+
for (var v : cnt.values()) {
90+
if (v.size() == 1) {
91+
return v.get(0);
92+
}
93+
}
94+
return "";
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
string oddString(vector<string>& words) {
105+
unordered_map<string, vector<string>> cnt;
106+
for (auto& w : words) {
107+
string d;
108+
for (int i = 0; i < w.size() - 1; ++i) {
109+
d += (char) (w[i + 1] - w[i]);
110+
d += ',';
111+
}
112+
cnt[d].emplace_back(w);
113+
}
114+
for (auto& [_, v] : cnt) {
115+
if (v.size() == 1) {
116+
return v[0];
117+
}
118+
}
119+
return "";
120+
}
121+
};
122+
```
73123
124+
### **Go**
125+
126+
```go
127+
func oddString(words []string) string {
128+
cnt := map[string][]string{}
129+
for _, w := range words {
130+
d := make([]byte, len(w)-1)
131+
for i := 0; i < len(w)-1; i++ {
132+
d[i] = w[i+1] - w[i]
133+
}
134+
t := string(d)
135+
cnt[t] = append(cnt[t], w)
136+
}
137+
for _, v := range cnt {
138+
if len(v) == 1 {
139+
return v[0]
140+
}
141+
}
142+
return ""
143+
}
74144
```
75145

76146
### **TypeScript**

solution/2400-2499/2451.Odd String Difference/README_EN.md

+71-1
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,83 @@ The odd array out is [1, 1], so we return the corresponding string, &quot;abc&qu
5454
### **Python3**
5555

5656
```python
57-
57+
class Solution:
58+
def oddString(self, words: List[str]) -> str:
59+
cnt = defaultdict(list)
60+
for w in words:
61+
d = [str(ord(b) - ord(a)) for a, b in pairwise(w)]
62+
cnt[','.join(d)].append(w)
63+
return next(v[0] for v in cnt.values() if len(v) == 1)
5864
```
5965

6066
### **Java**
6167

6268
```java
69+
class Solution {
70+
public String oddString(String[] words) {
71+
Map<String, List<String>> cnt = new HashMap<>();
72+
for (var w : words) {
73+
List<String> d = new ArrayList<>();
74+
for (int i = 0; i < w.length() - 1; ++i) {
75+
d.add(String.valueOf(w.charAt(i + 1) - w.charAt(i)));
76+
}
77+
cnt.computeIfAbsent(String.join(",", d), k -> new ArrayList<>()).add(w);
78+
}
79+
for (var v : cnt.values()) {
80+
if (v.size() == 1) {
81+
return v.get(0);
82+
}
83+
}
84+
return "";
85+
}
86+
}
87+
```
88+
89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
string oddString(vector<string>& words) {
95+
unordered_map<string, vector<string>> cnt;
96+
for (auto& w : words) {
97+
string d;
98+
for (int i = 0; i < w.size() - 1; ++i) {
99+
d += (char) (w[i + 1] - w[i]);
100+
d += ',';
101+
}
102+
cnt[d].emplace_back(w);
103+
}
104+
for (auto& [_, v] : cnt) {
105+
if (v.size() == 1) {
106+
return v[0];
107+
}
108+
}
109+
return "";
110+
}
111+
};
112+
```
63113
114+
### **Go**
115+
116+
```go
117+
func oddString(words []string) string {
118+
cnt := map[string][]string{}
119+
for _, w := range words {
120+
d := make([]byte, len(w)-1)
121+
for i := 0; i < len(w)-1; i++ {
122+
d[i] = w[i+1] - w[i]
123+
}
124+
t := string(d)
125+
cnt[t] = append(cnt[t], w)
126+
}
127+
for _, v := range cnt {
128+
if len(v) == 1 {
129+
return v[0]
130+
}
131+
}
132+
return ""
133+
}
64134
```
65135

66136
### **TypeScript**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
string oddString(vector<string>& words) {
4+
unordered_map<string, vector<string>> cnt;
5+
for (auto& w : words) {
6+
string d;
7+
for (int i = 0; i < w.size() - 1; ++i) {
8+
d += (char) (w[i + 1] - w[i]);
9+
d += ',';
10+
}
11+
cnt[d].emplace_back(w);
12+
}
13+
for (auto& [_, v] : cnt) {
14+
if (v.size() == 1) {
15+
return v[0];
16+
}
17+
}
18+
return "";
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func oddString(words []string) string {
2+
cnt := map[string][]string{}
3+
for _, w := range words {
4+
d := make([]byte, len(w)-1)
5+
for i := 0; i < len(w)-1; i++ {
6+
d[i] = w[i+1] - w[i]
7+
}
8+
t := string(d)
9+
cnt[t] = append(cnt[t], w)
10+
}
11+
for _, v := range cnt {
12+
if len(v) == 1 {
13+
return v[0]
14+
}
15+
}
16+
return ""
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public String oddString(String[] words) {
3+
Map<String, List<String>> cnt = new HashMap<>();
4+
for (var w : words) {
5+
List<String> d = new ArrayList<>();
6+
for (int i = 0; i < w.length() - 1; ++i) {
7+
d.add(String.valueOf(w.charAt(i + 1) - w.charAt(i)));
8+
}
9+
cnt.computeIfAbsent(String.join(",", d), k -> new ArrayList<>()).add(w);
10+
}
11+
for (var v : cnt.values()) {
12+
if (v.size() == 1) {
13+
return v.get(0);
14+
}
15+
}
16+
return "";
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def oddString(self, words: List[str]) -> str:
3+
cnt = defaultdict(list)
4+
for w in words:
5+
d = [str(ord(b) - ord(a)) for a, b in pairwise(w)]
6+
cnt[','.join(d)].append(w)
7+
return next(v[0] for v in cnt.values() if len(v) == 1)

0 commit comments

Comments
 (0)