Checking if two strings are permutations of each other

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

How to determine if two strings are permutations of each other

相关标签:
30条回答
  • 2020-12-05 08:51

    I did it using C#

    bool Arepermutations(string string1, string string2)
            {
                char[] str1 = string1.ToCharArray();
                char[] str2 = string2.ToCharArray();
                if (str1.Length !=str2.Length)
                  return false; 
                Array.Sort(str1); 
                Array.Sort(str2);
                if (str1.Where((t, i) => t!= str2[i]).Any())
                {
                    return false;
                }
    
                return true; 
    
            }
    
    0 讨论(0)
  • 2020-12-05 08:52
    1. Sort the 2 strings by characters and compare if they're the same (O(n log n) time, O(n) space), or
    2. Tally the character frequency of the 2 strings and compare if they're the same (O(n) time, O(n) space).
    0 讨论(0)
  • 2020-12-05 08:52

    You might take a look at String.toCharArray and Arrays.sort

    0 讨论(0)
  • 2020-12-05 08:52

    As you requested, here's a complete solution using recursion. Now all you have to do is:

    1. Figure out what language this is
    2. Translate it to Java.

    Good luck :-)

    proc isAnagram(s1, s2);
      return {s1, s2} = {''} or
             (s2 /= '' and
              (exists c = s1(i) |
                      s2(1) = c and
                      isAnagram(s1(1..i-1) + s1(i+1..), s2(2..))));
    end isAnagram;
    
    0 讨论(0)
  • 2020-12-05 08:53

    Best Way to do this is by sorting the two strings first and then compare them. It's not the most efficient way but it's clean and is bound to the runtime of the sorting routine been used.

    boolean arePermutation(String s1, String s2) { 
      if(s1.lenght() != s2.lenght()) {
         return false;
       }
       return mySort(s1).equals(mySort(s2));
     }
    
      String mySort(String s) {
       Char letters[] = s.toCharArray();
       Arrays.sort(letters);
       return new String(letters);
    }
    
    0 讨论(0)
  • 2020-12-05 08:54
    public boolean isPermutationOfOther(String str, String other){
        if(str == null || other == null)
            return false;
        if(str.length() != other.length())
            return false;
    
        Map<Character, Integer> characterCount = new HashMap<Character, Integer>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            int count = 1;
            if(characterCount.containsKey(c)){
                int k = characterCount.get(c);
                count = count+k;
            }
    
            characterCount.put(c, count);
    
        }
    
        for (int i = 0; i < other.length(); i++) {
            char c = other.charAt(i);
            if(!characterCount.containsKey(c)){
                return false;
            }
    
            int count = characterCount.get(c);
            if(count == 1){
                characterCount.remove(c);
            }else{
                characterCount.put(c, --count);
            }
        }
    
        return characterCount.isEmpty();
    }
    
    0 讨论(0)
提交回复
热议问题