-
Notifications
You must be signed in to change notification settings - Fork 277
/
PermutationInString.java
107 lines (82 loc) · 2.57 KB
/
PermutationInString.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
class Solution {
public boolean checkInclusion(String t, String s) {
List<Integer> ans = new ArrayList<>();
int unmatchingCharCount = t.length();
if(t.length()>s.length()){
return false;
}
int[] freqCount = new int[26];
for(int i=0;i<t.length();i++){
int index = t.charAt(i)-'a';
freqCount[index]++;
}
int start =0;
int end =0;
for(;end<t.length();end++) {
int index = s.charAt(end)-'a';
if(freqCount[index]>0){
unmatchingCharCount--;
}
freqCount[index]--;
}
if(unmatchingCharCount==0){
return true;
}
for(;end<s.length();){
// remove starting index
int indexStart = s.charAt(start) -'a';
if(freqCount[indexStart]>=0) {
// char was present in t
unmatchingCharCount++;
}
freqCount[indexStart]++;
start++;
int indexEnd = s.charAt(end)-'a';
if(freqCount[indexEnd]>0){
unmatchingCharCount--;
}
freqCount[indexEnd]--;
end++;
if(unmatchingCharCount==0){
return true;
}
}
return false;
}
}
// @saorav21994
// TC : O(l1+l1+(l2-l1)) = O(l1+ l2)
// SC : O(26+26) = O(52) = Constant
/*
Sliding window technique is used.
*/
class Solution {
public boolean checkInclusion(String s1, String s2) {
if (s1.length() > s2.length()) return false;
int [] hash1 = new int[26];
int [] hash2 = new int[26];
for (char ch : s1.toCharArray()) {
hash1[(int)ch-97] += 1;
}
int sublen = s1.length();
int matlen = 0;
for (int i = 0; i < sublen; i++) {
hash2[(int)s2.charAt(i)-97] += 1;
if (hash1[(int)s2.charAt(i)-97] >= hash2[(int)s2.charAt(i)-97]) matlen += 1;
}
if (matlen == sublen) return true;
int biglen = s2.length();
for (int i = sublen ; i < biglen; i++) {
if (hash1[(int)s2.charAt(i-sublen)-97] >= hash2[(int)s2.charAt(i-sublen)-97]) {
matlen -= 1;
}
hash2[(int)s2.charAt(i-sublen)-97] -= 1;
hash2[(int)s2.charAt(i)-97] += 1;
if (hash1[(int)s2.charAt(i)-97] >= hash2[(int)s2.charAt(i)-97]) {
matlen += 1;
}
if (matlen == sublen) return true;
}
return false;
}
}