53 lines
1.2 KiB
Java
53 lines
1.2 KiB
Java
/*
|
|
* @lc app=leetcode id=567 lang=java
|
|
*
|
|
* [567] Permutation in String
|
|
*/
|
|
|
|
// @lc code=start
|
|
class Solution {
|
|
public boolean checkInclusion(String s1, String s2) {
|
|
if(s1.length() > s2.length()) {
|
|
return false;
|
|
}
|
|
int[] c1 = strCompose(s1);
|
|
int[] c2 = strCompose(s2.substring(0, s1.length()));
|
|
boolean f = isComposeSame(c1, c2);
|
|
int j = s1.length();
|
|
while(!f && j < s2.length()) {
|
|
c2[s2.charAt(j - s1.length())-'a']--;
|
|
c2[s2.charAt(j)-'a']++;
|
|
if(isComposeSame(c1, c2)) {
|
|
f = true;
|
|
break;
|
|
}
|
|
j++;
|
|
}
|
|
return f;
|
|
}
|
|
|
|
private int[] strCompose(String s) {
|
|
int[] c = new int[26];
|
|
for(int i = 0; i < s.length(); i++) {
|
|
c[s.charAt(i)-'a']++;
|
|
}
|
|
return c;
|
|
}
|
|
|
|
private boolean isComposeSame(int[] a, int[] b) {
|
|
if(a.length != b.length) {
|
|
return false;
|
|
}
|
|
boolean matches = true;
|
|
for(int i=0; i<a.length; i++) {
|
|
if(a[i] != b[i]) {
|
|
matches = false;
|
|
break;
|
|
}
|
|
}
|
|
return matches;
|
|
}
|
|
}
|
|
// @lc code=end
|
|
|