【问题标题】:Find all possible combinations of letters from two "alphabets" for any given word查找任何给定单词的两个“字母表”中所有可能的字母组合
【发布时间】:2021-06-29 21:37:00
【问题描述】:

我得到了这两个“字母”数组:

private static final char[] regularAlphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private static final char[] secondAlphabet = {'ᴀ', 'ʙ', 'ᴄ', 'ᴅ', 'ᴇ', 'ꜰ', 'ɢ', 'ʜ', 'ɪ', 'ᴊ', 'ᴋ', 'ʟ', 'ᴍ', 'ɴ', 'ᴏ', 'ᴘ', 'q', 'ʀ', 'ꜱ', 'ᴛ', 'ᴜ', 'ᴠ', 'ᴡ', 'x', 'ʏ', 'ᴢ' };

secondAlphabet 使用特殊字符,因此它们的读取方式与常规字符不同。

我打算做的是打印可以为给定字符串完成的所有可能组合。例如,给定字符串“ab”,我们可以得到:ab, ᴀb, aʙ, ᴀʙ(注意从每个“字母”中提取的字符的差异)。

单词将保持不变,因此字母不会重新排列,只是要找到该单词的这些类型的字母之间的所有可能组合。此外,使用诸如 toUpperCase 之类的方法也行不通,因为我需要使用在 secondArray 中找到的这些特殊字符。

我需要什么样的循环才能找到解决方案?

【问题讨论】:

    标签: java math logic


    【解决方案1】:

    编辑:不是正确答案

    您也许可以像这样使用双 for 循环:

    for (char ch1 : regularAlphabet) {
        for (char ch2 : secondAlphabet) {
            System.out.println(String.valueOf(ch1) + String.valueOf(ch2));
        }
    }
    

    或者更好的是,优化的 for 循环(可读性较差)

    for (int i = 0; i < regularAlphabet.length * secondAlphabet.length; i++) {
        System.out.println(String.valueOf(regularAlphabet[i % regularAlphabet.length]) + String.valueOf(secondAlphabet[(int) (i / regularAlphabet.length)]));
    }
    

    【讨论】:

    • 我希望能够输入任何单词并从两个字母中获得所有可能的字母组合,而不是简单地获得所有 2 个字母组合
    • 抱歉,我误会了。
    猜你喜欢
    • 1970-01-01
    • 2022-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多