Checking if two strings are permutations of each other

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

How to determine if two strings are permutations of each other

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

    If we do the initial length check, to determine if the two strings are of the same length or not,

        public boolean checkIfPermutation(String str1, String str2) {
          if(str1.length()!=str2.length()){
            return false;
          }
          int str1_sum = getSumOfChars(str1);
          int str2_sum = getSumOfChars(str2);
          if (str1_sum == str2_sum) {
            return true;
          }
          return false;
    }
    
    public int getSumOfChars(String str){
          int sum = 0; 
          for (int i = 0; i < str.length(); i++) {
            sum += str.charAt(i);
          }
          return sum;
    }
    
    0 讨论(0)
  • 2020-12-05 08:56
        String str= "abcd";
        String str1 ="dcazb";
        int count=0;
    
        char s[]= str.toCharArray();
        char s1[]= str1.toCharArray();
    
        for(char c:s)
        {
            count = count+(int)c ;
        }
    
        for(char c1:s1)
        {
            count=count-(int)c1;
    
        }
    
        if(count==0)
        System.out.println("String are Anagram");
        else
        System.out.println("String are not Anagram");
    
    }
    
    0 讨论(0)
  • 2020-12-05 08:58

    I did this, and it works well and quickly:

    public static boolean isPermutation(String str1, String str2)
    {
        char[] x = str1.toCharArray();
        char[] y = str2.toCharArray();
        Arrays.sort(x);
        Arrays.sort(y);
        if(Arrays.equals(x, y))
            return true;
        return false;
    }
    
    0 讨论(0)
  • 2020-12-05 09:00

    Linear Time solution in HashMap. Traverse and put first String in HashMap, keep the count of each character. Traverse second String and if it is already in the hashmap decrease the count by 1. At the end if all character were in the string the value in hashmap will be 0 for each character.

    public class StringPermutationofEachOther {
        public static void main(String[] args)
        {
    
        String s1= "abc";
        String s2 ="bbb";
        System.out.println(perm(s1,s2));
    }
        public static boolean perm(String s1, String s2)
        { HashMap<Character, Integer> map = new HashMap<Character, Integer>();
        int count =1;
            if(s1.length()!=s2.length())
            {
                return false;
            }
                for(Character c: s1.toCharArray())
                {
                    if(!map.containsKey(c))
                        map.put(c, count);
    
                    else
                        map.put(c, count+1);
    
                }
                for(Character c: s2.toCharArray())
                {
                    if(!map.containsKey(c))
                        return false;
    
                    else
                        map.put(c, count-1);
                }
                for(Character c: map.keySet())
                {
                    if(map.get(c)!=0)
                        return false;
                }
            return true;
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-05 09:00

    The obligatory Guava one-liner:

    boolean isAnagram(String s1, String s2) {
        return ImmutableMultiset.copyOf(Chars.asList(s1.toCharArray())).equals(ImmutableMultiset.copyOf(Chars.asList(s2.toCharArray())));
    }
    

    (Just for fun. I don't recommend submitting this for your assignment.)

    0 讨论(0)
  • 2020-12-05 09:02

    First you check the lengths (n), if they are not same, they cannot be permutations of each other. Now create two HashMap<Character, Integer>. Iterate over each string and put the number of times each character occur in the string. E.g. if the string is aaaaa, the map will have just one element with key a and value 5. Now check if the two maps are identical. This is an O(n) algorithm.

    EDIT with code snippet :

    boolean checkPermutation(String str1, String str2) {
    
    char[] chars1 = str1.toCharArray();
    char[] chars2 = str2.toCharArray();
    
    Map<Character, Integer> map1 = new HashMap<Character, Integer>();
    Map<Character, Integer> map2 = new HashMap<Character, Integer>();
    
    for (char c : chars1) {
       int occ = 1;
       if (map1.containsKey(c) {
           occ = map1.get(c);
           occ++;
       }
       map1.put(c, occ);
    }
    
    // now do the same for chars2 and map2
    
    if (map1.size() != map2.size()) {
       return false;
    }
    for (char c : map1.keySet()) {
    
        if (!map2.containsKey(c) || map1.get(c) != map2.get(c)) {
            return false;
        }
    }
    
    return true;
    }
    
    0 讨论(0)
提交回复
热议问题