Checking if 2 strings contain the same characters?

前端 未结 10 900
孤城傲影
孤城傲影 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:08

    Maybe it's not the fastest answer, but it must be shortest answer.

    boolean hasSameChar(String str1, String str2){
      for(char c : str1.toCharArray()){
        if(str2.indexOf(c) < 0 ) return false;
      }
      for(char c : str2.toCharArray()){
        if(str1.indexOf(c) < 0 ) return false;
      }
      return true;
    }
    

提交回复
热议问题