File tree Expand file tree Collapse file tree 2 files changed +78
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 2 files changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution :
2+ """
3+ @param: strs: a list of strings
4+ @return: encodes a list of strings to a single string.
5+ """
6+ def encode (self , strs ):
7+ # write your code here
8+ result = ""
9+
10+ delimiter = ":;"
11+
12+ for i , str in enumerate (strs ):
13+ result = result + str
14+ if i != len (strs ):
15+ result = result + delimiter
16+
17+ print (result )
18+
19+ return result
20+
21+ """
22+ @param: str: A string
23+ @return: decodes a single string to a list of strings
24+ """
25+ def decode (self , str ):
26+ result = []
27+
28+ i = 0
29+ delimiter = ":;"
30+
31+ while i < len (str ):
32+
33+ j = i
34+ while j < len (str ) - 1 :
35+ if str [j :j + 2 ] == delimiter :
36+ result .append (str [i :j ])
37+ break
38+ else :
39+ j = j + 1
40+
41+ i = j + 2
42+
43+ return result
Original file line number Diff line number Diff line change 1+ # TC : O(m^2 * n) m : len(strs), n : len(strs[0])
2+ # SC : O(m * n)
3+
4+ class Solution :
5+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
6+ sorted_strs = []
7+ for str in strs :
8+ sorted_strs .append (sorted (str ))
9+
10+ answer = []
11+
12+ i = 0
13+
14+ visited = [False ] * 10001
15+
16+ while i < len (strs ):
17+ if visited [i ]:
18+ i = i + 1
19+ continue
20+
21+ sub_answer = []
22+ target = sorted_strs [i ]
23+ sub_answer .append (strs [i ])
24+
25+ for j in range (i + 1 , len (strs )):
26+ if sorted_strs [j ] == target :
27+ visited [j ] = True
28+ sub_answer .append (strs [j ])
29+
30+ i = i + 1
31+ answer .append (sub_answer )
32+
33+ return answer
34+
35+
You can’t perform that action at this time.
0 commit comments