-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweightedSubstring.java
44 lines (33 loc) · 1.14 KB
/
weightedSubstring.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
import java.util.HashSet;
import java.util.Scanner;
public class weightedSubstring {
private static int getWeightedSubstrings(String p, String q, int k) {
HashSet<String> h = new HashSet<>();
for (int i = 0; i < p.length(); i++) {
String current = "";
int cost = 0;
for (int j = i; j < p.length(); j++) {
current += p.charAt(j);
cost += q.charAt(p.charAt(j) - 'a') - '0';
if (cost <= k) {
System.out.println(current);
h.add(current);
} else {
break;
}
}
}
return h.size();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the string: ");
String p = in.nextLine();
System.out.print("Enter the weights: ");
String q = in.nextLine();
System.out.print("Enter the max substring weight: ");
int k = in.nextInt();
System.out.println("Strings is: " + getWeightedSubstrings(p, q, k));
in.close();
}
}