首页 > 代码库 > Well-ordered String
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 }
Well-ordered String
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。