DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Java
  4. Java Anagram Buster

Java Anagram Buster

Sandeep Bhandari user avatar by
Sandeep Bhandari
·
Mar. 18, 12 · Interview
Like (0)
Save
Tweet
Share
10.01K Views

Join the DZone community and get the full member experience.

Join For Free

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.

 

Java (programming language) Strings

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Steel Threads Are a Technique That Will Make You a Better Engineer
  • Leverage Lambdas for Cleaner Code
  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding
  • Scaling Your Testing Efforts With Cloud-Based Testing Tools

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: