Skip to content

Commit 8aa44a4

Browse files
committed
feat: add solutions to lc problem: No.0893
No.0893.Groups of Special-Equivalent Strings
1 parent a015979 commit 8aa44a4

File tree

9 files changed

+257
-8
lines changed

9 files changed

+257
-8
lines changed

basic/sorting/InsertionSort/InsertionSort.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ def insertion_sort(array):
22
for i in range(len(array)):
33
cur_index = i
44
while array[cur_index - 1] > array[cur_index] and cur_index - 1 >= 0:
5-
array[cur_index], array[cur_index - 1] = array[cur_index - 1], array[cur_index]
5+
array[cur_index], array[cur_index - 1] = (
6+
array[cur_index - 1],
7+
array[cur_index],
8+
)
69
cur_index -= 1
710
return array
811

12+
913
array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
1014
print(insertion_sort(array))

solution/0000-0099/0054.Spiral Matrix/Solution.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
77
ans.extend([matrix[top][j] for j in range(left, right + 1)])
88
ans.extend([matrix[i][right] for i in range(top + 1, bottom + 1)])
99
if left < right and top < bottom:
10-
ans.extend([matrix[bottom][j]
11-
for j in range(right - 1, left - 1, -1)])
12-
ans.extend([matrix[i][left]
13-
for i in range(bottom - 1, top, -1)])
10+
ans.extend([matrix[bottom][j] for j in range(right - 1, left - 1, -1)])
11+
ans.extend([matrix[i][left] for i in range(bottom - 1, top, -1)])
1412
top, bottom, left, right = top + 1, bottom - 1, left + 1, right - 1
1513
return ans

solution/0300-0399/0361.Bomb Enemy/Solution.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ def maxKilledEnemies(self, grid: List[List[str]]) -> int:
3232
elif grid[i][j] == 'E':
3333
t += 1
3434
g[i][j] += t
35-
return max([g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'], default=0)
35+
return max(
36+
[g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'],
37+
default=0,
38+
)

solution/0800-0899/0893.Groups of Special-Equivalent Strings/README.md

+85-1
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,99 @@
7070
<!-- 这里可写当前语言的特殊实现逻辑 -->
7171

7272
```python
73-
73+
class Solution:
74+
def numSpecialEquivGroups(self, words: List[str]) -> int:
75+
s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words}
76+
return len(s)
7477
```
7578

7679
### **Java**
7780

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

8083
```java
84+
class Solution {
85+
public int numSpecialEquivGroups(String[] words) {
86+
Set<String> s = new HashSet<>();
87+
for (String word : words) {
88+
s.add(convert(word));
89+
}
90+
return s.size();
91+
}
92+
93+
private String convert(String word) {
94+
List<Character> a = new ArrayList<>();
95+
List<Character> b = new ArrayList<>();
96+
for (int i = 0; i < word.length(); ++i) {
97+
char ch = word.charAt(i);
98+
if (i % 2 == 0) {
99+
a.add(ch);
100+
} else {
101+
b.add(ch);
102+
}
103+
}
104+
Collections.sort(a);
105+
Collections.sort(b);
106+
StringBuilder sb = new StringBuilder();
107+
for (char c : a) {
108+
sb.append(c);
109+
}
110+
for (char c : b) {
111+
sb.append(c);
112+
}
113+
return sb.toString();
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
int numSpecialEquivGroups(vector<string>& words) {
124+
unordered_set<string> s;
125+
for (auto& word : words)
126+
{
127+
string a = "", b = "";
128+
for (int i = 0; i < word.size(); ++i)
129+
{
130+
if (i & 1) a += word[i];
131+
else b += word[i];
132+
}
133+
sort(a.begin(), a.end());
134+
sort(b.begin(), b.end());
135+
s.insert(a + b);
136+
}
137+
return s.size();
138+
}
139+
};
140+
```
81141
142+
### **Go**
143+
144+
```go
145+
func numSpecialEquivGroups(words []string) int {
146+
s := map[string]bool{}
147+
for _, word := range words {
148+
a, b := []rune{}, []rune{}
149+
for i, c := range word {
150+
if i&1 == 1 {
151+
a = append(a, c)
152+
} else {
153+
b = append(b, c)
154+
}
155+
}
156+
sort.Slice(a, func(i, j int) bool {
157+
return a[i] < a[j]
158+
})
159+
sort.Slice(b, func(i, j int) bool {
160+
return b[i] < b[j]
161+
})
162+
s[string(a)+string(b)] = true
163+
}
164+
return len(s)
165+
}
82166
```
83167

84168
### **...**

solution/0800-0899/0893.Groups of Special-Equivalent Strings/README_EN.md

+85-1
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,97 @@ Note that in particular, &quot;zzxy&quot; is not special equivalent to &quot;zzy
5959
### **Python3**
6060

6161
```python
62-
62+
class Solution:
63+
def numSpecialEquivGroups(self, words: List[str]) -> int:
64+
s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words}
65+
return len(s)
6366
```
6467

6568
### **Java**
6669

6770
```java
71+
class Solution {
72+
public int numSpecialEquivGroups(String[] words) {
73+
Set<String> s = new HashSet<>();
74+
for (String word : words) {
75+
s.add(convert(word));
76+
}
77+
return s.size();
78+
}
79+
80+
private String convert(String word) {
81+
List<Character> a = new ArrayList<>();
82+
List<Character> b = new ArrayList<>();
83+
for (int i = 0; i < word.length(); ++i) {
84+
char ch = word.charAt(i);
85+
if (i % 2 == 0) {
86+
a.add(ch);
87+
} else {
88+
b.add(ch);
89+
}
90+
}
91+
Collections.sort(a);
92+
Collections.sort(b);
93+
StringBuilder sb = new StringBuilder();
94+
for (char c : a) {
95+
sb.append(c);
96+
}
97+
for (char c : b) {
98+
sb.append(c);
99+
}
100+
return sb.toString();
101+
}
102+
}
103+
```
104+
105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int numSpecialEquivGroups(vector<string>& words) {
111+
unordered_set<string> s;
112+
for (auto& word : words)
113+
{
114+
string a = "", b = "";
115+
for (int i = 0; i < word.size(); ++i)
116+
{
117+
if (i & 1) a += word[i];
118+
else b += word[i];
119+
}
120+
sort(a.begin(), a.end());
121+
sort(b.begin(), b.end());
122+
s.insert(a + b);
123+
}
124+
return s.size();
125+
}
126+
};
127+
```
68128
129+
### **Go**
130+
131+
```go
132+
func numSpecialEquivGroups(words []string) int {
133+
s := map[string]bool{}
134+
for _, word := range words {
135+
a, b := []rune{}, []rune{}
136+
for i, c := range word {
137+
if i&1 == 1 {
138+
a = append(a, c)
139+
} else {
140+
b = append(b, c)
141+
}
142+
}
143+
sort.Slice(a, func(i, j int) bool {
144+
return a[i] < a[j]
145+
})
146+
sort.Slice(b, func(i, j int) bool {
147+
return b[i] < b[j]
148+
})
149+
s[string(a)+string(b)] = true
150+
}
151+
return len(s)
152+
}
69153
```
70154

71155
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int numSpecialEquivGroups(vector<string>& words) {
4+
unordered_set<string> s;
5+
for (auto& word : words)
6+
{
7+
string a = "", b = "";
8+
for (int i = 0; i < word.size(); ++i)
9+
{
10+
if (i & 1) a += word[i];
11+
else b += word[i];
12+
}
13+
sort(a.begin(), a.end());
14+
sort(b.begin(), b.end());
15+
s.insert(a + b);
16+
}
17+
return s.size();
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
func numSpecialEquivGroups(words []string) int {
2+
s := map[string]bool{}
3+
for _, word := range words {
4+
a, b := []rune{}, []rune{}
5+
for i, c := range word {
6+
if i&1 == 1 {
7+
a = append(a, c)
8+
} else {
9+
b = append(b, c)
10+
}
11+
}
12+
sort.Slice(a, func(i, j int) bool {
13+
return a[i] < a[j]
14+
})
15+
sort.Slice(b, func(i, j int) bool {
16+
return b[i] < b[j]
17+
})
18+
s[string(a)+string(b)] = true
19+
}
20+
return len(s)
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution {
2+
public int numSpecialEquivGroups(String[] words) {
3+
Set<String> s = new HashSet<>();
4+
for (String word : words) {
5+
s.add(convert(word));
6+
}
7+
return s.size();
8+
}
9+
10+
private String convert(String word) {
11+
List<Character> a = new ArrayList<>();
12+
List<Character> b = new ArrayList<>();
13+
for (int i = 0; i < word.length(); ++i) {
14+
char ch = word.charAt(i);
15+
if (i % 2 == 0) {
16+
a.add(ch);
17+
} else {
18+
b.add(ch);
19+
}
20+
}
21+
Collections.sort(a);
22+
Collections.sort(b);
23+
StringBuilder sb = new StringBuilder();
24+
for (char c : a) {
25+
sb.append(c);
26+
}
27+
for (char c : b) {
28+
sb.append(c);
29+
}
30+
return sb.toString();
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def numSpecialEquivGroups(self, words: List[str]) -> int:
3+
s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words}
4+
return len(s)

0 commit comments

Comments
 (0)