Set集合避免重复,一个字符串插入单个字符,枚举26个字符,复杂度26o(n),然后看评论区,我真是菜啊,排列组合:(字符串长度n+1)26-n
重复的次数恰好是字符串长度
import java.util.*; public class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); Set<String> set = new HashSet<>(); for(int i = 0; i < 26; i++){ char temp = (char) ('a' + i); for(int j = 0; j <= s.length(); j++){ StringBuilder stringBuilder = new StringBuilder(s); stringBuilder.insert(j, temp); set.add(stringBuilder.toString()); } } System.out.println(set.size()); } }