Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from typing import List, Any
- class Solution:
- def anagrams(self,strs):
- strs_comp = strs.copy()
- if len(strs_comp) == 0:
- return [] # trivial case
- else:
- answer = [] # we keep an answer array to store all sorted anagrams
- k = 0
- strs_comp = sorted(strs_comp).copy()
- original: List[Any] = strs_comp.copy() # save for later output
- for i in range(len(original)):
- if strs_comp[i] != '':
- strs_comp[i] = ''.join(sorted(strs_comp[i]))
- #we sort everything, hold the length 0 exception
- while k < len(strs_comp):
- #start to iterate
- if strs_comp[k] not in answer:
- if strs_comp[k] in strs_comp[k+1:]:
- answer.append(strs_comp[k])
- k+=1
- #match but not in answer, update answer and next
- else:
- #not match not in answer
- strs_comp.pop(k)
- original.pop(k)
- else:
- k+=1
- #got something in answer, next
- return original
Add Comment
Please, Sign In to add comment