File tree Expand file tree Collapse file tree 1 file changed +4
-8
lines changed
Python/chapter01/1.2 - Check Perm Expand file tree Collapse file tree 1 file changed +4
-8
lines changed Original file line number Diff line number Diff line change @@ -28,16 +28,12 @@ def check_permutation(s1: str, s2: str) -> bool:
28
28
:param s2: string of size m
29
29
:return: True if s1 is a permutation of s2, False otherwise
30
30
"""
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
32
35
# using histogram because string could have repeated characters
33
36
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
-
41
37
freqs_s2 = collections .Counter (s2 )
42
38
# compare frequencies of characters for s1 and s2
43
39
for key , val in freqs_s1 .items ():
You can’t perform that action at this time.
0 commit comments