Checking if two strings are permutations of each other

前端 未结 30 1007
感情败类
感情败类 2020-12-05 08:19

How to determine if two strings are permutations of each other

30条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-05 09:04

    To put @Michael Borgwardt's words in to code:

    public boolean checkAnagram(String str1, String str2) {
    
        if (str1.length() != str2.length())
          return false;
    
        char[] a = str1.toCharArray();
        char[] b = str2.toCharArray();
    
        Arrays.sort(a);
        Arrays.sort(b);
    
        return Arrays.equals(a, b);
    }
    

提交回复
热议问题