-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalidPhoneNumber
43 lines (34 loc) · 1.1 KB
/
validPhoneNumber
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
/*
Print all valid phone numbers of length n subject to following constraints:
1.If a number contains a 4, it should start with 4
2.No two consecutive digits can be same
3.Three digits (e.g. 7,2,9) will be entirely disallowed, take as input
*/
private static void validPhoneNumber(int d, int n, int s, String result, String pattern){
if (d == n){
if (checkvalid(result))
System.out.println(result);
return;
}
else {
for (int i=0; i<pattern.length(); i++){
if (!keys.containsKey(i)) {
//keys.put(i, true); // controls if the same digit is allowed to show up for more than one time.
validPhoneNumber(d+1, n, i+1, result+i, patttern); // s controls if this is combination or permutation
//keys.remove(i);
}
}
}
return;
}
private static boolean checkvalid(String result){
int count = 0;
for (int i=1; i<result.length(); i++){
if (result.charAt(i) == '4') count++;
if (result.charAt(i) == result.charAt(i-1))
return false;
}
return(count==0);
}
private static String pattern = "0134568";
private static Hashtable<Integer, Boolean> keys = new Hashtable<Integer, Boolean>();