Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0893
Browse files Browse the repository at this point in the history
No.0893.Groups of Special-Equivalent Strings
  • Loading branch information
yanglbme committed Apr 30, 2022
1 parent a015979 commit 8aa44a4
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 8 deletions.
6 changes: 5 additions & 1 deletion basic/sorting/InsertionSort/InsertionSort.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ def insertion_sort(array):
for i in range(len(array)):
cur_index = i
while array[cur_index - 1] > array[cur_index] and cur_index - 1 >= 0:
array[cur_index], array[cur_index - 1] = array[cur_index - 1], array[cur_index]
array[cur_index], array[cur_index - 1] = (
array[cur_index - 1],
array[cur_index],
)
cur_index -= 1
return array


array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
print(insertion_sort(array))
6 changes: 2 additions & 4 deletions solution/0000-0099/0054.Spiral Matrix/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
ans.extend([matrix[top][j] for j in range(left, right + 1)])
ans.extend([matrix[i][right] for i in range(top + 1, bottom + 1)])
if left < right and top < bottom:
ans.extend([matrix[bottom][j]
for j in range(right - 1, left - 1, -1)])
ans.extend([matrix[i][left]
for i in range(bottom - 1, top, -1)])
ans.extend([matrix[bottom][j] for j in range(right - 1, left - 1, -1)])
ans.extend([matrix[i][left] for i in range(bottom - 1, top, -1)])
top, bottom, left, right = top + 1, bottom - 1, left + 1, right - 1
return ans
5 changes: 4 additions & 1 deletion solution/0300-0399/0361.Bomb Enemy/Solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ def maxKilledEnemies(self, grid: List[List[str]]) -> int:
elif grid[i][j] == 'E':
t += 1
g[i][j] += t
return max([g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'], default=0)
return max(
[g[i][j] for i in range(m) for j in range(n) if grid[i][j] == '0'],
default=0,
)
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,99 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

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

### **Java**

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

```java
class Solution {
public int numSpecialEquivGroups(String[] words) {
Set<String> s = new HashSet<>();
for (String word : words) {
s.add(convert(word));
}
return s.size();
}

private String convert(String word) {
List<Character> a = new ArrayList<>();
List<Character> b = new ArrayList<>();
for (int i = 0; i < word.length(); ++i) {
char ch = word.charAt(i);
if (i % 2 == 0) {
a.add(ch);
} else {
b.add(ch);
}
}
Collections.sort(a);
Collections.sort(b);
StringBuilder sb = new StringBuilder();
for (char c : a) {
sb.append(c);
}
for (char c : b) {
sb.append(c);
}
return sb.toString();
}
}
```

### **C++**

```cpp
class Solution {
public:
int numSpecialEquivGroups(vector<string>& words) {
unordered_set<string> s;
for (auto& word : words)
{
string a = "", b = "";
for (int i = 0; i < word.size(); ++i)
{
if (i & 1) a += word[i];
else b += word[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
s.insert(a + b);
}
return s.size();
}
};
```
### **Go**
```go
func numSpecialEquivGroups(words []string) int {
s := map[string]bool{}
for _, word := range words {
a, b := []rune{}, []rune{}
for i, c := range word {
if i&1 == 1 {
a = append(a, c)
} else {
b = append(b, c)
}
}
sort.Slice(a, func(i, j int) bool {
return a[i] < a[j]
})
sort.Slice(b, func(i, j int) bool {
return b[i] < b[j]
})
s[string(a)+string(b)] = true
}
return len(s)
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,97 @@ Note that in particular, &quot;zzxy&quot; is not special equivalent to &quot;zzy
### **Python3**

```python

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

### **Java**

```java
class Solution {
public int numSpecialEquivGroups(String[] words) {
Set<String> s = new HashSet<>();
for (String word : words) {
s.add(convert(word));
}
return s.size();
}

private String convert(String word) {
List<Character> a = new ArrayList<>();
List<Character> b = new ArrayList<>();
for (int i = 0; i < word.length(); ++i) {
char ch = word.charAt(i);
if (i % 2 == 0) {
a.add(ch);
} else {
b.add(ch);
}
}
Collections.sort(a);
Collections.sort(b);
StringBuilder sb = new StringBuilder();
for (char c : a) {
sb.append(c);
}
for (char c : b) {
sb.append(c);
}
return sb.toString();
}
}
```

### **C++**

```cpp
class Solution {
public:
int numSpecialEquivGroups(vector<string>& words) {
unordered_set<string> s;
for (auto& word : words)
{
string a = "", b = "";
for (int i = 0; i < word.size(); ++i)
{
if (i & 1) a += word[i];
else b += word[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
s.insert(a + b);
}
return s.size();
}
};
```
### **Go**
```go
func numSpecialEquivGroups(words []string) int {
s := map[string]bool{}
for _, word := range words {
a, b := []rune{}, []rune{}
for i, c := range word {
if i&1 == 1 {
a = append(a, c)
} else {
b = append(b, c)
}
}
sort.Slice(a, func(i, j int) bool {
return a[i] < a[j]
})
sort.Slice(b, func(i, j int) bool {
return b[i] < b[j]
})
s[string(a)+string(b)] = true
}
return len(s)
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int numSpecialEquivGroups(vector<string>& words) {
unordered_set<string> s;
for (auto& word : words)
{
string a = "", b = "";
for (int i = 0; i < word.size(); ++i)
{
if (i & 1) a += word[i];
else b += word[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
s.insert(a + b);
}
return s.size();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
func numSpecialEquivGroups(words []string) int {
s := map[string]bool{}
for _, word := range words {
a, b := []rune{}, []rune{}
for i, c := range word {
if i&1 == 1 {
a = append(a, c)
} else {
b = append(b, c)
}
}
sort.Slice(a, func(i, j int) bool {
return a[i] < a[j]
})
sort.Slice(b, func(i, j int) bool {
return b[i] < b[j]
})
s[string(a)+string(b)] = true
}
return len(s)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public int numSpecialEquivGroups(String[] words) {
Set<String> s = new HashSet<>();
for (String word : words) {
s.add(convert(word));
}
return s.size();
}

private String convert(String word) {
List<Character> a = new ArrayList<>();
List<Character> b = new ArrayList<>();
for (int i = 0; i < word.length(); ++i) {
char ch = word.charAt(i);
if (i % 2 == 0) {
a.add(ch);
} else {
b.add(ch);
}
}
Collections.sort(a);
Collections.sort(b);
StringBuilder sb = new StringBuilder();
for (char c : a) {
sb.append(c);
}
for (char c : b) {
sb.append(c);
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Solution:
def numSpecialEquivGroups(self, words: List[str]) -> int:
s = {''.join(sorted(word[::2]) + sorted(word[1::2])) for word in words}
return len(s)

0 comments on commit 8aa44a4

Please sign in to comment.