-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatchString.java
More file actions
26 lines (23 loc) · 811 Bytes
/
MatchString.java
File metadata and controls
26 lines (23 loc) · 811 Bytes
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
/* Given a string composed of 0, 1 and ?, ? can match 0 or 1, return all
* matching strings
* http://www.mitbbs.com/article_t/JobHunting/32505211.html
* http://www.mitbbs.com/article_t/JobHunting/32496747.html
*/
public class MatchString {
public static void main(String[] args) {
String str = "10?101?10";
printMatch(str, 0, "");
}
public static void printMatch(String str, int index, String buffer) {
if(index >= str.length()) {
System.out.println(buffer);
return;
}
if(str.charAt(index)=='1' || str.charAt(index)=='0')
printMatch(str, index+1, buffer+str.charAt(index));
else {
printMatch(str, index+1, buffer+"0");
printMatch(str, index+1, buffer+"1");
}
}
}