finding if two words are anagrams of each other

后端 未结 22 1058
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 14:51

I am looking for a method to find if two strings are anagrams of one another.

Ex: string1 - abcde
string2 - abced
Ans = true
Ex: string1 - abcde
string2 - ab         


        
相关标签:
22条回答
  • 2020-11-27 15:19

    How about converting into the int value of the character and sum up :

    If the value of sum are equals then they are anagram to each other.

    def are_anagram1(s1, s2):
        return [False, True][sum([ord(x) for x in s1]) == sum([ord(x) for x in s2])]
    
    s1 = 'james'
    s2 = 'amesj'
    print are_anagram1(s1,s2)
    

    This solution works only for 'A' to 'Z' and 'a' to 'z'.

    0 讨论(0)
  • 2020-11-27 15:20

    implementation in Swift 3:

    func areAnagrams(_ str1: String, _ str2: String) -> Bool {
        return dictionaryMap(forString: str1) == dictionaryMap(forString: str2)
    }
    
    func dictionaryMap(forString str: String) -> [String : Int] {
        var dict : [String : Int] = [:]
        for var i in 0..<str.characters.count {
            if let count = dict[str[i]] {
                dict[str[i]] = count + 1
            }else {
                dict[str[i]] = 1
            }
        }        
        return dict
    }
    //To easily subscript characters
    extension String {
        subscript(i: Int) -> String {
            return String(self[index(startIndex, offsetBy: i)])
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:21

    C#

    public static bool AreAnagrams(string s1, string s2)
    {
      if (s1 == null) throw new ArgumentNullException("s1");
      if (s2 == null) throw new ArgumentNullException("s2");
    
      var chars = new Dictionary<char, int>();
      foreach (char c in s1)
      {
          if (!chars.ContainsKey(c))
              chars[c] = 0;
          chars[c]++;
      }
      foreach (char c in s2)
      {
          if (!chars.ContainsKey(c))
              return false;
          chars[c]--;
      }
    
      return chars.Values.All(i => i == 0);
    }
    

    Some tests:

    [TestMethod]
    public void TestAnagrams()
    {
      Assert.IsTrue(StringUtil.AreAnagrams("anagramm", "nagaramm"));
      Assert.IsTrue(StringUtil.AreAnagrams("anzagramm", "nagarzamm"));
      Assert.IsTrue(StringUtil.AreAnagrams("anz121agramm", "nag12arz1amm"));
      Assert.IsFalse(StringUtil.AreAnagrams("anagram", "nagaramm"));
      Assert.IsFalse(StringUtil.AreAnagrams("nzagramm", "nagarzamm"));
      Assert.IsFalse(StringUtil.AreAnagrams("anzagramm", "nag12arz1amm"));
    }
    
    0 讨论(0)
  • 2020-11-27 15:23

    Count the frequency of each character in the two strings. Check if the two histograms match. O(n) time, O(1) space (assuming ASCII) (Of course it is still O(1) space for Unicode but the table will become very large).

    0 讨论(0)
  • 2020-11-27 15:25

    Well you can probably improve the best case and average case substantially just by checking the length first, then a quick checksum on the digits (not something complex, as that will probably be worse order than the sort, just a summation of ordinal values), then sort, then compare.

    If the strings are very short the checksum expense will be not greatly dissimilar to the sort in many languages.

    0 讨论(0)
  • 2020-11-27 15:25
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Scanner;
    
    /**
     * --------------------------------------------------------------------------
     * Finding Anagrams in the given dictionary. Anagrams are words that can be
     * formed from other words Ex :The word "words" can be formed using the word
     * "sword"
     * --------------------------------------------------------------------------
     * Input : if choose option 2 first enter no of word want to compare second
     * enter word ex:
     * 
     * Enter choice : 1:To use Test Cases 2: To give input 2 Enter the number of
     * words in dictionary 
     * 6
     * viq 
     * khan
     * zee 
     * khan
     * am
     *    
     * Dictionary : [ viq khan zee khan am]
     * Anagrams 1:[khan, khan]
     * 
     */
    public class Anagrams {
    
        public static void main(String args[]) {
            // User Input or just use the testCases
            int choice;
            @SuppressWarnings("resource")
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter choice : \n1:To use Test Cases 2: To give input");
            choice = scan.nextInt();
            switch (choice) {
            case 1:
                testCaseRunner();
                break;
            case 2:
                userInput();
            default:
                break;
            }
        }
    
        private static void userInput() {
            @SuppressWarnings("resource")
            Scanner scan = new Scanner(System.in);
            System.out.println("Enter the number of words in dictionary");
            int number = scan.nextInt();
            String dictionary[] = new String[number];
            //
            for (int i = 0; i < number; i++) {
                dictionary[i] = scan.nextLine();
            }
            printAnagramsIn(dictionary);
    
        }
    
        /**
         * provides a some number of dictionary of words
         */
        private static void testCaseRunner() {
    
            String dictionary[][] = { { "abc", "cde", "asfs", "cba", "edcs", "name" },
                    { "name", "mane", "string", "trings", "embe" } };
            for (int i = 0; i < dictionary.length; i++) {
                printAnagramsIn(dictionary[i]);
            }
        }
    
        /**
         * Prints the set of anagrams found the give dictionary
         * 
         * logic is sorting the characters in the given word and hashing them to the
         * word. Data Structure: Hash[sortedChars] = word
         */
        private static void printAnagramsIn(String[] dictionary) {
            System.out.print("Dictionary : [");// + dictionary);
            for (String each : dictionary) {
                System.out.print(each + " ");
            }
            System.out.println("]");
            //
    
            Map<String, ArrayList<String>> map = new LinkedHashMap<String, ArrayList<String>>();
            // review comment: naming convention: dictionary contains 'word' not
            // 'each'
            for (String each : dictionary) {
                char[] sortedWord = each.toCharArray();
                // sort dic value
                Arrays.sort(sortedWord);
                //input word
                String sortedString = new String(sortedWord);
                //
                ArrayList<String> list = new ArrayList<String>();
                if (map.keySet().contains(sortedString)) {
                    list = map.get(sortedString);
                }
                list.add(each);
                map.put(sortedString, list);
            }
            // print anagram
            int i = 1;
            for (String each : map.keySet()) {
                if (map.get(each).size() != 1) {
                    System.out.println("Anagrams " + i + ":" + map.get(each));
                    i++;
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题