string permutation with upcase and lowcase

Give a string, which only contains a-z. List all the permutation of upcase and lowcase. 
For example, str = "ab",  the output should be 
"ab", "aB", "Ab", "AB" 
for str = "abc", the output should be 
"abc", "abC", "aBc", "aBC", "Abc", "AbC", "ABc", "ABC" 

[解题思路]

本题与其他permutation题目区别在于结果中每位的字符都是固定的,仅仅是大小写的区别

因而不需要循环来遍历,使字符串的每一位可以遍历任意一个值

 1 public class Solution {
 2     public static void main(String[] args) {
 3         List<String> result = new ArrayList<String>();
 4         String tmp = "";
 5         permutation(tmp, 0, 4, result);
 6         System.out.println(result);
 7     }
 8     private static void permutation(String tmp, int depth, int len,
 9             List<String> result) {
10         if (depth == len) {
11             result.add(tmp);
12             return;
13         }
14 
15         tmp += (char) ('a' + depth);
16         permutation(tmp, depth + 1, len, result);
17         tmp = tmp.substring(0, tmp.length() - 1);
18 
19         tmp += (char) ('A' + depth);
20         permutation(tmp, depth + 1, len, result);
21         tmp = tmp.substring(0, tmp.length() - 1);
22 
23     }
24 }
原文地址:https://www.cnblogs.com/feiling/p/3307526.html