给定一个整数字符串,找出所有可以以连续顺序生成的可能单词。 [英] Given a string of integers find out all the possible words that can made out of it in continuous order.
本文介绍了给定一个整数字符串,找出所有可以以连续顺序生成的可能单词。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出一个整数字符串,以找出所有可以连续产生的单词。例如:11112
Given a string of integers how to find out all the possible words that can made out of it in continuous order. Eg: 11112
ans:AAAAB
AKAB
AAKB
AAAL等。
ans: AAAAB AKAB AAKB AAAL etc.
public static void main(String[] args) {
String str="11111124";
char strChar[]=str.toCharArray();
String target="";
for(int i=0;i<strChar.length;i++)
{
target=target+(char)Integer.parseInt(""+(16+strChar[i]));
}
System.out.println(target);
}
我正在尝试找到解决方案,但无法找到所有组合
i am trying to find the solution for this but not able to find all combination
推荐答案
结合注释说 163
可以是 1,6,3
或 16,3
,但不是 1,63
,以及 user3437460 的建议使用递归:
Combining the comments saying that 163
can be 1,6,3
or 16,3
, but not 1,63
, and user3437460's suggestion of using recursion:
- 取第一位数字并转换为字母。使用字母和剩余数字进行递归调用。
- 取前两位数字。如果
< = 26
,则转换为字母并使用字母和其余数字进行递归调用。
- Take first digit and convert to letter. Make recursive call using letter and remaining digits.
- Take first two digits. If
<=26
, convert to letter and make recursive call using letter and remaining digits.
这是代码。由于我不知道该调用什么方法,因此我将使用 x
。
Here is the code. Since I have no clue what to call the method, I'm going with x
.
public static void main(String[] args) {
x("11112", "");
System.out.println("------");
x("163", "");
}
private static final void x(String digits, String word) {
if (digits.isEmpty())
System.out.println(word);
else {
int num = Integer.parseInt(digits.substring(0, 1));
x(digits.substring(1), word + (char)('A' + num - 1));
if (digits.length() >= 2 && (num = Integer.parseInt(digits.substring(0, 2))) <= 26)
x(digits.substring(2), word + (char)('A' + num - 1));
}
}
输出
AAAAB
AAAL
AAKB
AKAB
AKL
KAAB
KAL
KKB
------
AFC
PC
这篇关于给定一个整数字符串,找出所有可以以连续顺序生成的可能单词。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文