forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0893
No.0893.Groups of Special-Equivalent Strings
- Loading branch information
Showing
9 changed files
with
257 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; |
21 changes: 21 additions & 0 deletions
21
solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
32 changes: 32 additions & 0 deletions
32
solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |