#567
Permutation in String
medium· Sliding Windowruns: 0Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise. In other words, return true if one of s1's permutations is the substring of s2.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 607
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if len(s1) > len(s2):
return False
s1_count = [0] * 26
s2_count = [0] * 26
for i in range(len(s1)):
s1_count[ord(s1[i]) - ord("a")] += 1
s2_count[ord(s2[i]) - ord("a")] += 1
if s1_count == s2_count:
return True
for i in range(len(s1), len(s2)):
s2_count[ord(s2[i]) - ord("a")] += 1
s2_count[ord(s2[i - len(s1)]) - ord("a")] -= 1
if s1_count == s2_count:
return True
return False
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.