Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6b9d989

Browse files
Adding Python Solution for 49.Group Anagrams and 242.Valid Anagrams (#135)
* Added Python solution for 242.Valid Anagram * Added Python solution for 49.Group Anagrams
1 parent 881a0bc commit 6b9d989

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(s) != len(t):
4+
return False
5+
6+
count_t = {}
7+
count_s = {}
8+
for i in range(len(t)):
9+
count_t[t[i]] = count_t.get(t[i], 0) + 1
10+
count_s[s[i]] = count_s.get(s[i], 0) + 1
11+
12+
for key in count_s.keys():
13+
if count_t.get(key) == None or count_t[key] != count_s[key]:
14+
return False
15+
16+
return True
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
grp_anagrams = {}
4+
5+
for string in strs:
6+
count = [0] * 26
7+
8+
for char in string:
9+
count[ord(char) - ord("a")] += 1
10+
11+
count = tuple(count)
12+
13+
if grp_anagrams.get(count) == None:
14+
grp_anagrams[count] = []
15+
16+
grp_anagrams[count].append(string)
17+
18+
return grp_anagrams.values()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /