Search This Blog

Wednesday, October 12, 2016

Algorithms. How to check if two words are anagrams (Java)

What is anagram? 
Any word or phrase that exactly reproduces the letters in another order is an anagram. (Wikipedia)
Example of anagrams: DOG, GOD, DGO, GDO, OGD, ODG, 

Task: 
to determine whether two strings anagrams.

Solution: 
1) make both strings similar case - use toLowesrCase() method 
2) Convert each string to char array
3) Use HashMap - for each character in first char array calculate number of repeats. And after that for second char array for each character decrease count of repeats. So if two string are anagrams than we have to get hash map object where for each character we have zero value.


Source code:
import java.util.HashMap;
import java.util.*;

public class Main {

  static boolean isAnagram(String a, String b) {
  
      HashMap<Character, Integer> hash = new HashMap();
      char[] arr1 = a.toLowerCase().toCharArray();
      char[] arr2 = b.toLowerCase().toCharArray();

      for (char c : arr1) {
        int count = 1;
        if (hash.containsKey(c)) {
          count = hash.get(c) + 1;
        }
        hash.put(c, count);
      }

      for (char c : arr2) {
        int count = -1;
        if (hash.containsKey(c)) {
          count = hash.get(c) - 1;
        }
      hash.put(c, count);
      }

      for (char c : hash.keySet()) {
        if (hash.get(c) != 0) {
          return false;
        }
      }

      return true;
  }

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    String a = scan.next();
    String b = scan.next();
    scan.close();
    boolean ret = isAnagram(a, b);
    System.out.println((ret) ? "Anagrams" : "Not Anagrams");
  }
}



Results:

Test 1:
Hello
olleh
Anagrams

Test 2:
Hello
helll
Not Anagrams

Test 3:
Morning
gninromm
Not Anagrams

Test 4:
Programming
mmggrrpoain
Anagrams

7 comments:

  1. Really nice article. its really helpful me. Very interesting and good post thanks for sharing such a good blog.
    -Web Development Services

    ReplyDelete
  2. A big shout out to you, such a useful and thought provoking article on Custom WebsiteA straight from an intellectual mind

    ReplyDelete
  3. This post wowed me with its well-researched content and excellent writing. Because I was so involved in this book, I couldn't put it down. Your work and ability have wowed me. Thank you very much. Those who want to learn more about it may find it useful. Best Custom Websites

    ReplyDelete
  4. Thank you very much for drawing my notice to this. Your blog is jam-packed with useful facts. Until I read that, I would have no idea. I'll come back for more fantastic material.
    Best wishes, and good luck.
    Web Design USA

    ReplyDelete
  5. Good content. I was looking for this type of content. Saved my time for further search.
    You should always provide that type of content. I appreciate your determination to give the students a good title.Custom Designed Websites

    ReplyDelete
  6. Thank you so much for your post; it has provided us with an excellent idea.
    SEO Firm USA

    ReplyDelete
  7. Amazing write-up! The blog was very informative. Keep it up!
    Custom Website Development

    ReplyDelete