Day 14 Task:
2. Permutation in String: https://leetcode.com/problems/permutation-in-string/
Explanation: https://www.youtube.com/watch?v=UbyhOgBN834
Solutions:
Please review all the solutions and try implementing on your own if you haven’t gotten it.
class Solution:
def checkInclusion(self, s1: str, s2: str) -> bool:
if(len(s2) < len(s1)):
return False
alp_1 = {}
alp_2 = {}
for i in range(26):
alp_1[chr(97+i)] = 0
alp_2[chr(97+i)] = 0
for i in range(len(s1)):
alp_1[s1[i]] += 1
alp_2[s2[i]] += 1
matches = 0
for i in range(26):
if(alp_1[chr(97+i)] == alp_2[chr(97+i)]):
matches += 1
l = 0
for r in range(len(s1), len(s2)):
if(matches == 26):
return True
alp_2[s2[l]] -= 1
if(alp_2[s2[l]] == alp_1[s2[l]]):
matches += 1
elif(alp_1[s2[l]]-1 == alp_2[s2[l]]):
matches -= 1
l += 1
alp_2[s2[r]] += 1
if(alp_2[s2[r]] == alp_1[s2[r]]):
matches += 1
elif(alp_1[s2[r]] + 1 == alp_2[s2[r]]):
matches -= 1
if(matches == 26):
return True
return False