Well-ordered String

Problem

You know a password is well-ordered string. Well-ordered string means that the order of the characters is in an alphabetical increasing order. Like “abEm” is a well-ordered number. However, “abmE” is not a well-order number. Given an input# that tells you also how many digits are in the password, print all possible 

Solution

Recursive + DFS

 1     public static List<List<String>> hackCode(int num) {
 2         List<List<String>> res = new ArrayList<List<String>>();
 3         if (num < 0) {
 4             return res;
 5         }
 6 
 7         List<String> ls = new ArrayList<String>();
 8         helper(res, ls, 0, num, 0);
 9         return res;
10     }
11 
12     public static void helper(List<List<String>> res, List<String> ls, int prev,
13             int num, int pos) {
14         if (pos == num) {
15             res.add(new ArrayList<String>(ls));
16             return;
17         }
18 
19         for (int i = prev; i < 26; i++) {
20             char c = (char) ('a' + i);
21             ls.add(String.valueOf(c));
22             helper(res, ls, i + 1, num, pos + 1);
23             ls.remove(ls.size() - 1);
24         }
25     }
原文地址:https://www.cnblogs.com/superbo/p/4111922.html