Skip to content

Commit 2754de5

Browse files
committed
Adding Simple Solution for Problem - 49 - Group Anagrams
1 parent debae72 commit 2754de5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Group_Anagrams/EfficientSolution.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 49
6+
## Problem Name: Group Anagrams
7+
##===================================
8+
#
9+
#Given an array of strings, group anagrams together.
10+
#
11+
#Example:
12+
#
13+
#Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
14+
#Output:
15+
#[
16+
# ["ate","eat","tea"],
17+
# ["nat","tan"],
18+
# ["bat"]
19+
#]
20+
class Solution:
21+
def groupAnagrams(self, strs):
22+
tmp = dict() #Initialize tmp empty dictionary
23+
for word in strs: #Loop through strs list
24+
sortWord = "".join(sorted(word)) #Initialize sortWord which will sort the word letters and return in a string format
25+
if sortWord in dict: #Condition-check:If sortWord is in dictionary
26+
tmp[sortWord].append(word) #We'll append the word in sortWord key
27+
else: #Condition-check: Else
28+
tmp[sortWord] = [word] #We'll make a new key and add value as a list
29+
anagrams = [] #Initialize anagrams empty list which we'll return
30+
for i in tmp.values(): #Loop through tmp values
31+
anagrams.append(i) #Append it to the anagrams list
32+
return anagrams #Return anagrams list
33+

0 commit comments

Comments
 (0)