-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sort.java
47 lines (37 loc) · 1.08 KB
/
Sort.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
package java8;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Sort{
static int find_sub(String s, int k)
{
int len = s.length();
// initialize left and right pointer to 0
int lp = 0, rp = 0;
int ans = 0;
// an array to keep track of count of each alphabet
int[] hash_char = new int[26];
for (; rp < len; rp++)
{
hash_char[s.charAt(rp) - 'a']++;
while (hash_char[s.charAt(rp) - 'a'] > k)
{
// decrement the count
hash_char[s.charAt(lp) - 'a']--;
//increment left pointer
lp++;
}
ans += rp - lp + 1;
}
return ans;
}
public static void main(String[] args) {
String S = "cycle";
int k = 1;
System.out.println(find_sub(S, k));
}
}