-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet11.java
More file actions
39 lines (39 loc) · 823 Bytes
/
leet11.java
File metadata and controls
39 lines (39 loc) · 823 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public boolean checkInclusion(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
if(n1>n2)
return false;
if(n1==0)
return true;
int a[] = new int[26];
int b[] = new int[26];
for(int i=0; i<n1; i++)
{
a[s1.charAt(i)-'a']++;
b[s2.charAt(i)-'a']++;
}
int freq = 0;
for(int i=0; i<26; i++)
if(a[i]==b[i])
freq++;
for(int i=0; i<n2-n1; i++)
{
if(freq==26)
return true;
int left = s2.charAt(i)-'a';
int right = s2.charAt(i+n1)-'a';
b[right]++;
if(a[right]==b[right])
freq++;
else if(a[right]+1==b[right])
freq--;
b[left]--;
if(a[left]==b[left])
freq++;
else if(a[left]-1==b[left])
freq--;
}
return freq==26;
}
}