Skip to content

Commit 7199d92

Browse files
committed
final solution Leetcode 49
1 parent 0f7d7e1 commit 7199d92

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

Steve_Implementation/leetcode worked solutions.ipynb

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,51 +76,36 @@
7676
},
7777
{
7878
"cell_type": "code",
79-
"execution_count": 1,
79+
"execution_count": 49,
8080
"metadata": {},
8181
"outputs": [
8282
{
8383
"data": {
8484
"text/plain": [
85-
"[['tea', 'ate', 'tan'], ['ate'], ['nat'], ['bat']]"
85+
"[['tan', 'nat'], ['eat', 'tea', 'ate'], ['bat']]"
8686
]
8787
},
88-
"execution_count": 1,
88+
"execution_count": 49,
8989
"metadata": {},
9090
"output_type": "execute_result"
9191
}
9292
],
9393
"source": [
9494
"### Leetcode 49\n",
9595
"\n",
96-
"strs = [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]\n",
97-
"\n",
98-
"l = []\n",
99-
"ll = []\n",
100-
"a = 0\n",
101-
"b = a + 1\n",
102-
"while a != len(strs):\n",
103-
" if a == len(strs) - 1:\n",
104-
" ll.append([strs[a]])\n",
105-
" break\n",
106-
"\n",
107-
" elif sorted(strs[a]) == sorted(strs[b]):\n",
108-
" l.append(strs[b])\n",
109-
" strs.remove(strs[a])\n",
110-
"\n",
111-
" b += 1\n",
112-
"\n",
113-
" if b == len(strs):\n",
114-
" l.append(strs[a])\n",
115-
" ll.append(l)\n",
116-
" l = []\n",
96+
"class Solution(object):\n",
97+
" def groupAnagrams(self, strs):\n",
98+
" \"\"\"\n",
99+
" :type strs: List[str]\n",
100+
" :rtype: List[List[str]]\n",
101+
" \"\"\" \n",
117102
"\n",
118-
" a += 1\n",
119-
" b = a + 1\n",
103+
" hash = {j:[] for j in set([''.join(sorted(i)) for i in strs])}\n",
120104
"\n",
121-
"ll\n",
105+
" for i in strs:\n",
106+
" hash[''.join(sorted(i))].append(i)\n",
122107
"\n",
123-
" "
108+
" return list(hash.values())\n"
124109
]
125110
}
126111
],

0 commit comments

Comments
 (0)