-
Notifications
You must be signed in to change notification settings - Fork 0
/
Count_of_substrings.java
55 lines (55 loc) · 1.44 KB
/
Count_of_substrings.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
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{int left=0,right=0;
int k=s.nextInt();
char cc=s.next().charAt(0);
String c=s.next();
int n=c.length();
HashMap<Integer,Integer>hm1=new HashMap<>();
HashMap<Integer,Integer>hm2=new HashMap<>();
ArrayList<Integer>al=new ArrayList<>();
int a[]=new int[256];
a[cc]=k;
int count=0,ans=0;
for(right=0;right<n;right++)
{ans=0;
if(--a[c.charAt(right)]>=0)
count++;
if(count==k)
al.add(right);
while(count==k)
{ans++;
hm1.put(right,ans);
if(++a[c.charAt(left)]>0)
count--;
left++;
}}
count=0;
a[cc]=k;
right=left=n-1;
ans=0;
for(right=n-1;right>=0;right--)
{
ans=0;
if(--a[c.charAt(right)]>=0)
count++;
boolean flag=false;
while(count==k)
{ans++;flag=true;
if(++a[c.charAt(left)]>0)
count--;
left--;}
if(flag)
hm2.put(left+1,ans);
}
ans=0;
if(al.size()==0)
{}
else
{for(Integer i:al)
ans+=hm1.get(i)*hm2.get(i);}
System.out.println(ans);
}
}