Skip to content

Commit c05ab3d

Browse files
committed
bring back length check, removed a code block
Removed code block that checks that for each character in s2 there exists at least one occurrence in s1. See PR discussion for more details.
1 parent c682bb5 commit c05ab3d

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

Python/chapter01/1.2 - Check Perm/miguel_1.2_sol.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,12 @@ def check_permutation(s1: str, s2: str) -> bool:
2828
:param s2: string of size m
2929
:return: True if s1 is a permutation of s2, False otherwise
3030
"""
31-
# build histogram of seen characters in s1
31+
# precondition for a permutation, must be same length
32+
if len(s1) != len(s2):
33+
return False
34+
# build histogram of seen characters in s1 and s2
3235
# using histogram because string could have repeated characters
3336
freqs_s1 = collections.Counter(s1)
34-
35-
# check if s2 characters exist in s1
36-
for c in s2:
37-
# if character in s2 not in s1, then this is not a permutation
38-
if c not in freqs_s1:
39-
return False
40-
4137
freqs_s2 = collections.Counter(s2)
4238
# compare frequencies of characters for s1 and s2
4339
for key, val in freqs_s1.items():

0 commit comments

Comments
 (0)