Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java Anagram Buster

DZone's Guide to

Java Anagram Buster

· Java Zone
Free Resource

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

While browsing the net, I found this problem somewhere – to write a code that tests the given two strings are anagrams or not. From Wiki,

“An anagram is a type of word play, the result of rearranging the letter of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example orchestra can be rearranged into carthorse.”

I tried to solve this problem using Java and below is the result of it. The algorithm I tried is very simple:

  1. Clean the input – remove all the spaces and punctuation marks (because it doesn’t affect the compassion).
  2. Go through character by character from string one and check if that character exists in string two.
  3. If exists, then remove it from string two and move on to next character. If not exists, then we found a mismatch and the string is not an anagram.
  4. If all character from string one exists in string two, then we found it’s an anagram.

 Java code to test two strings are anagrams:

public class AnagramTester {

	public static void main(String[] args) {
		String one = "The United States of America";
		String two = "Attaineth its cause, freedom";
		System.out.println(new AnagramTester().test(one, two));
	}

	public boolean test(String a, String b) {

		boolean result = true;

		StringBuilder one = new StringBuilder(a.replaceAll("[\\s+\\W+]", "").toLowerCase());
		StringBuilder two = new StringBuilder(b.replaceAll("[\\s+\\W+]", "").toLowerCase());
		
		if (one.length() == two.length()) {

			int index = -1;

			for (char c : one.toString().toCharArray()) {

				index = two.indexOf(String.valueOf(c));

				if (index == -1) {
					result = false;
					break;
				}
				two.deleteCharAt(index);
			}
		} else {
			result = false;
		}

		return result;

	}

}

 

I tested the above code with most of the anagrams found in the Anagram Site and it worked well.

If you think the above code can be improved in someway, feel free to comment.

 

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Sandeep Bhandari. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}