Skip to content

Commit 250b477

Browse files
committed
Add permutation in string
1 parent 0158e87 commit 250b477

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def check_inclusion(s1: str, s2: str) -> bool:
2+
pattern_hash = {}
3+
for character in s1:
4+
if character not in pattern_hash:
5+
pattern_hash[character] = 1
6+
else:
7+
pattern_hash[character] += 1
8+
9+
hash_copy = pattern_hash.copy()
10+
pattern_length = len(s1)
11+
12+
for index in range(len(s2)):
13+
if index >= pattern_length:
14+
outgoing_character = s2[index - pattern_length]
15+
if outgoing_character in pattern_hash:
16+
if outgoing_character not in hash_copy:
17+
hash_copy[outgoing_character] = 1
18+
else:
19+
hash_copy[outgoing_character] += 1
20+
if hash_copy[outgoing_character] == 0:
21+
del hash_copy[outgoing_character]
22+
23+
incoming_character = s2[index]
24+
if incoming_character in pattern_hash:
25+
if incoming_character in hash_copy:
26+
hash_copy[incoming_character] -= 1
27+
else:
28+
hash_copy[incoming_character] = -1
29+
if hash_copy[incoming_character] == 0:
30+
del hash_copy[incoming_character]
31+
if len(hash_copy) == 0:
32+
return True
33+
return False
34+
35+
36+
assert check_inclusion(s1='ab', s2='eidbaooo') == True, 'Test 1 Failed'
37+
assert check_inclusion(s1='ab', s2='eidboaoo') == False, 'Test 2 Failed'
38+
assert check_inclusion(s1='adc', s2='dcda') == True, 'Test 3 Failed'

0 commit comments

Comments
 (0)