我的程序打印包含双字母的单词的所有排列例如在单词'Helloo'中
结果将是:[heloo, helo, hello, hello]
private List<String> doubleLetterPermute(String start, String ending, List<String> possibleWords) {
String prev = "";
for (int i = 0; i < ending.length(); i++) {
String letter = String.valueOf(ending.charAt(i));
// its a double letter
if (letter.equals(prev)) {
doubleLetterPermute(start + ending.substring(0, i), ending.substring(i + 1), possibleWords);
doubleLetterPermute(start + ending.substring(0, i + 1), ending.substring(i + 1), possibleWords);
}
prev = letter;
}
possibleWords.add(start + ending);
return possibleWords;
}
我不确定当递归函数出现时如何计算复杂度。它循环 n 次,其中 n 是单词中的字符数,O(n),但我在每次出现双字母时拆分单词所以这在大 O 符号中是如何显示的?