-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupAnagram.py
40 lines (32 loc) · 913 Bytes
/
groupAnagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 8 01:24:17 2020
@author: zenit
Given an array of strings, group anagrams together.
"""
class Solution:
def groupAnagrams(self, strs):
"""
Parameters
----------
strs : List[str]
Returns
-------
List[List[str]]
"""
i = 0
strsFinal=[]
while i < len(strs):
strs1 = []
j = 0
while j < len(strs):
if sorted(strs[i]) == sorted(strs[j]):
strs1.append(strs[j])
j += 1
i += 1
if strs1 not in strsFinal:
strsFinal.append(strs1)
return strsFinal
s = Solution()
test = ["eat","tea","tan","ate","nat","bat"]
print(s.groupAnagrams(test))