Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Permutations of a given string
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
public class Solution {
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) {
return false;
}

int[] s1Count = new int[26];
int[] s2Count = new int[26];
for (int i = 0; i < s1.length(); i++) {
s1Count[s1.charAt(i) - 'a']++;
s2Count[s2.charAt(i) - 'a']++;
}

int matches = 0;
for (int i = 0; i < 26; i++) {
if (s1Count[i] == s2Count[i]) {
matches++;
}
}

int l = 0;
for (int r = s1.length(); r < s2.length(); r++) {
if (matches == 26) {
return true;
}

int index = s2.charAt(r) - 'a';
s2Count[index]++;
if (s1Count[index] == s2Count[index]) {
matches++;
} else if (s1Count[index] + 1 == s2Count[index]) {
matches--;
}

index = s2.charAt(l) - 'a';
s2Count[index]--;
if (s1Count[index] == s2Count[index]) {
matches++;
} else if (s1Count[index] - 1 == s2Count[index]) {
matches--;
}
l++;
}
return matches == 26;
}
}