命名单词组合

时间:2017-03-30 08:05:40

标签: string naming-conventions

假设" A B C D"作为一个句子,是否存在以下两个字符串解构的专用名称:

1)

尺寸:3< => {" ABC"," BCD"}

尺寸:2< => {" AB"," BC"," CD"}

2)

尺寸:3< => {" ABC"," ABD"," BCD"}

尺寸:2< => {" AB"," AC"," AD"," BC"," BD"," CD"}

请注意,我们永远不会置换元素......

提前谢谢你,

-Bast

1 个答案:

答案 0 :(得分:0)

与java代码一样

String sequence = "A B C D";

public List createConventions(String sequence,int mode) {
    List<String> result = new ArrayList<>();
    String[] elements = sequence.split(" ");
    for (int i = 0; i < elements.length; i++) {
        for (int j = i + 1; j < elements.length; j++) {
            if (mode == 1) {
                result.add(elements[i] + elements[j]);
            } else if (mode == 2) {
                for (int k = j + 1; k < elements.length; k++) {
                    result.add(elements[i] + elements[j] + elements[k]);
                }
            }
        }
    }
    return result;
}