Skip to content

Commit 5f8000d

Browse files
committed
Leetcode accepted
1 parent 5f91555 commit 5f8000d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Blind 75/Programs/Valid Anagram.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Valid Anagram
2+
# https://leetcode.com/problems/valid-anagram/
3+
4+
class Solution(object):
5+
def putInHash(self, s):
6+
hashTable = {}
7+
for char in s:
8+
if (char not in hashTable):
9+
hashTable[char] = 0
10+
hashTable[char] = hashTable[char] + 1
11+
return hashTable
12+
13+
def searchInHash(self, t, hashTable):
14+
# find the char in hash table.
15+
for char in t:
16+
if (char not in hashTable):
17+
return False
18+
hashTable[char] = hashTable[char] - 1
19+
# Make sure all count is zero in hash table
20+
for key in hashTable:
21+
if (hashTable[key] != 0):
22+
return False
23+
return True
24+
25+
def isAnagram(self, s, t):
26+
hashTable = self.putInHash(s)
27+
return self.searchInHash(t, hashTable)
28+
"""
29+
:type s: str
30+
:type t: str
31+
:rtype: bool
32+
"""
33+

Blind 75/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,10 @@
230230
</p>
231231
</li>
232232
<li>Minimum Window Substring</li>
233-
<li>Valid Anagram</li>
233+
<li><a href="Programs/Valid Anagram.py">Valid Anagram</a>
234+
<p><b>Approach</b>: Used hash table.
235+
</p>
236+
</li>
234237
<li>Group Anagrams</li>
235238
<li>Valid Parentheses</li>
236239
<li>Valid Palindrome</li>

0 commit comments

Comments
 (0)