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     }
时间: 2024-08-13 23:38:35

Well-ordered String的相关文章

String 经常用法最优算法实现总结 (二)

1. String getOrderedString(boolean isDuplicated, String - str) 说明: Orders all characters in the input strings and return the ordered string.(note: only considering the alphabets and digits) i.e: (false, {"ahcdx", "abcuy", "cejm&qu

String 常用方法最优算法实现总结 (二)

1. String getOrderedString(boolean isDuplicated, String - str) 说明: Orders all characters in the input strings and return the ordered string.(note: only considering the alphabets and digits) i.e: (false, {"ahcdx", "abcuy", "cejm&qu

Scala 深入浅出实战经典 第44讲: scala中view bounds代码实例

王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 腾讯微云:http://url.cn/TnGbdC 360云盘:http://yunpan.cn/cQ4c2UALDjSKy 访问密码 45e2 技术爱好者尤其是大数据爱好者 可以加DT大数据梦工厂的qq群 DT大数据梦工厂① :462923555 DT大数据梦工厂②:437123764 DT大数据梦工厂③

好程序员大数据学习路线分享Scala系列之泛型

好程序员大数据学习路线分享Scala系列之泛型,带有一个或多个类型参数的类是泛型的. 泛型类的定义: //带有类型参数A的类定义class Stack[A] {private var elements: List[A] = Nil//泛型方法def push(x: A) { elements = x :: elements }def peek: A = elements.headdef pop(): A = {val currentTop = peekelements = elements.ta

POJ 2533-Longest Ordered Subsequence(dp_最长上升子序列)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35502   Accepted: 15572 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

Longest Ordered Subsequence与最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)

Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N.

POJ2533-Longest Ordered Subsequence

描述: A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence (ai1, ai2,..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1,

poj 2533 Longest Ordered Subsequence(dp)

Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36159   Accepted: 15882 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence (a1, a2, ...

poj-2533 Longest Ordered Subsequence 【最长上升子序列】

题目链接:http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35929   Accepted: 15778 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the

N - Longest Ordered Subsequence POJ 2533 (最长上升子序列 )

N - Longest Ordered Subsequence Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2533 Description A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numer