Replace wild cards with all possible combinations of zeros and ones using recurs - 我的博客 - ITeye技术网站
Replace wild cards with all possible combinations of zeros and ones using recursion.
Read full article from Replace wild cards with all possible combinations of zeros and ones using recurs - 我的博客 - ITeye技术网站
Replace wild cards with all possible combinations of zeros and ones using recursion.
Input String: 0?1?
Output: 0010, 0011, 0110, 0111
- public List<String> replaceQuestionMark(String s) {
- List<String> result = new LinkedList<String>();
- replaceQuestionMarkRecursive(result, s.toCharArray(), 0);
- return result;
- }
- private void replaceQuestionMarkRecursive(List<String> result, char[] chars, int start) {
- if(start == chars.length) {
- result.add(new String(chars));
- return;
- }
- if(chars[start] == '?') {
- chars[start] = '0';
- replaceQuestionMarkRecursive(result, chars, start+1);
- chars[start] = '1';
- replaceQuestionMarkRecursive(result, chars, start+1);
- chars[start] = '?'; //backtrack
- } else {
- replaceQuestionMarkRecursive(result, chars, start+1);
- }
- }
Read full article from Replace wild cards with all possible combinations of zeros and ones using recurs - 我的博客 - ITeye技术网站