-
Notifications
You must be signed in to change notification settings - Fork 14
/
ABCD Strings.java
53 lines (51 loc) · 1.07 KB
/
ABCD Strings.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
import java.util.Scanner;
class ABCDString {
private int count=0;
String restrict[];
public ABCDString(String res[]) {
restrict = res;
}
public void countStrings(String prefix) {
int notMatch = 0;
for(int i=0;i<restrict.length;i++) {
if(!prefix.contains(restrict[i]) ) {
notMatch++;
}
}
if(notMatch == restrict.length) {
count++;
}
}
public int getCount() {
return count;
}
public void generateStrings(String prefix,int n) {
char set[] = {'a','b','c','d'};
if(n == 0) {
countStrings(prefix);
return;
}
for(int i=0;i<set.length;i++) {
String newPrefix = prefix + set[i];
generateStrings(newPrefix,n-1);
}
}
}
class Main {
public static void main(String cla[]) {
Scanner stdin = new Scanner(System.in );
int t,n,k;
t = stdin.nextInt();
while(t-- > 0) {
n = stdin.nextInt();
k = stdin.nextInt();
String restrict[] = new String[k];
for(int i=0;i<k;i++) {
restrict[i] = stdin.next();
}
ABCDString obj = new ABCDString(restrict);
obj.generateStrings("",n);
System.out.println(obj.getCount());
}
}
}