我想按字母顺序对JAVA中的字符串进行排序,如下所示,大写字母和小写字母跟随AaBbCcDdEeFfGg.例如,如果我把AbaC归还给我AabC谢谢!!
我写了这个类,可以检查两个给定的字符串是否是彼此的排列.但是,据我所知,这是在O(n ^ 2)时间运行,因为string.indexOf()在O(n)时间运行.
如何提高这项计划的效率?
import java.util.*;
public class IsPermutation{
public void IsPermutation(){
System.out.println("Checks if two strings are permutations of each other.");
System.out.println("Call the check() method");
}
public boolean check(){
Scanner console = new Scanner(System.in);
System.out.print("Insert first string: ");
String first = console.nextLine();
System.out.print("Insert second string: ");
String second = console.nextLine();
if (first.length() != second.length()){
System.out.println("Not permutations");
return false;
}
for (int i = 0; i < first.length(); i++){
if (second.indexOf(first.charAt(i)) == -1){
System.out.println("Not permutations");
return false;
}
}
System.out.println("permutations");
return …Run Code Online (Sandbox Code Playgroud)