Created
February 4, 2023 10:13
-
-
Save dluciano/dbb3fa4425f2342df3ecdfa298f10a8c to your computer and use it in GitHub Desktop.
567. Permutation in String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public bool CheckInclusion(string s1, string s2) { | |
if(s1.Length > s2.Length) | |
return false; | |
var s2Index = 0; | |
var s1Freq = new int[26]; | |
foreach(var letter in s1) | |
s1Freq[letter - 'a']++; | |
while(s2Index < s2.Length){ | |
if(s1Freq[s2[s2Index] - 'a'] != 0){ | |
var j = 0; | |
var s2Freq = new int[26]; | |
var found = true; | |
while(j < s1.Length && j + s2Index < s2.Length){ | |
var curKey = s2[j + s2Index] - 'a'; | |
s2Freq[curKey]++; | |
if(s2Freq[curKey] > s1Freq[curKey]){ | |
found = false; | |
break; | |
} | |
j++; | |
} | |
if(found){ | |
var freqAreEq = true; | |
for(var i = 0; i < s1Freq.Length; ++i){ | |
if(s1Freq[i] != s2Freq[i]){ | |
freqAreEq = false; | |
break; | |
} | |
} | |
if(freqAreEq) | |
return true; | |
} | |
} | |
s2Index++; | |
} | |
return false; | |
} | |
} | |
/* | |
"ccaab" | |
"eid - abcca - ooo" True | |
"ccaab" | |
"eid - <c[abcc>a] - ooo" True | |
"ccaab" | |
"eid - abccca - ooo" False | |
"cccaab" | |
"eid - abcca - ooo" False | |
"ccb" | |
"eid - acc - ooo" False | |
"eidbaooo" False | |
"ab" | |
"ccaab" | |
"eidabccaooo" | |
"ccaab" | |
"eidcabccaooo" | |
"ccaab" | |
"eidabcccaooo" | |
"cccaab" | |
"eidabccaooo" | |
"ccb" | |
"eidaccooo" | |
"eidbaooo" | |
"ab" | |
"ab" | |
"eidbaooo" | |
"ab" | |
"eidboaoo" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment