-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodSubstrings.java
More file actions
37 lines (35 loc) · 1.01 KB
/
GoodSubstrings.java
File metadata and controls
37 lines (35 loc) · 1.01 KB
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
/** #hash-table */
import java.util.HashSet;
import java.util.Scanner;
public class GoodSubstrings {
public static int BASE = 31;
public static long MOD = 9_223_372_036_854_775_807L;
public static void main(String[] args) {
// input
Scanner sc = new Scanner(System.in);
String s = sc.next();
String verifyString = sc.next();
int maxBadCharNum = sc.nextInt();
int sz = s.length();
// calculate
long hashCode;
int badCharNum, c;
HashSet<Long> hashSet = new HashSet<>();
for (int left = 0; left < sz; left++) {
hashCode = 0;
badCharNum = 0;
for (int right = left; right < sz; right++) {
c = s.charAt(right) - 'a';
hashCode = (c + 1 + (BASE * hashCode) % MOD) % MOD;
badCharNum += verifyString.charAt(c) == '0' ? 1 : 0;
if (badCharNum <= maxBadCharNum) {
hashSet.add(hashCode);
} else {
break;
}
}
}
long goodSubStrNum = hashSet.size();
System.out.println(goodSubStrNum);
}
}