Checking if 2 strings contain the same characters?

前端 未结 10 884
孤城傲影
孤城傲影 2020-12-10 04:49

Is there a way to check if two strings contain the same characters. For example,

abc, bca -> true
aaa, aaa -> true
aab, bba -> false
abc, def ->          


        
10条回答
  •  [愿得一人]
    2020-12-10 05:22

    Here:

    import java.util.Arrays;
    

    public class CompareString {

    String str = "Result";
    String str1 = "Struel";
    
    public void compare() {
        char[] firstString = str.toLowerCase().toCharArray();
        char[] secondString = str1.toLowerCase().toCharArray();
    
        Arrays.sort(firstString);
        Arrays.sort(secondString);
    
        if (Arrays.equals(firstString, secondString) == true) {
            System.out.println("Both the string contain same charecter");
        } else {
            System.out.println("Both the string contains different charecter");
        }
    }
    
    public static void main(String[] args) {
        CompareString compareString = new CompareString();
        compareString.compare();
    }
    

    }

提交回复
热议问题