Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:40:56+00:00 2026-05-25T23:40:56+00:00

I have been given an exercise about anagrams and it looked so really easy

  • 0

I have been given an exercise about anagrams and it looked so really easy that I have a doubt I am missing something.
The solution I implemented is the one I will present shortly, and I wanted to ask you if you could think of any optimization, change of approach or problem with my solution.
I implemented the algorithm in Java.

Now, the exercise.
As input I have a text and as output I should return whether each line of this text is an anagram of each other line.
That is, for input:

A Cab Deed Huffiest Minnows Loll
A Cab Deed Huffiest Minnow Lolls
A Cab Deed Shuffles Million Wont
A Cab Deed Shuffles Million Town

The program should return True.
For input:

A Cab Deed Huffiest Minnows Loll
A Cab Deed Huffiest Minnow Lolls hi
A Cab Deed Shuffles Million Wont
A Cab Deed Shuffles Million Town

the output will have to be False (because of the second line, of course).

Now, what I thought is pretty straightforward:

  • I create 2 HashMap: ref and cur.
  • I parse the first line of the text, filling ref. I will count only alphabetical letters.
  • for each other line, I parse the line into cur and check if cur.equals(ref): if so return false
  • if I get to the end of the text, it means that each line is an anagram of each other line, so I return true.

And…this would be it.
I tried it with an input text of 88000 lines, and it works pretty fast.

Any comments? Suggestions? Optimizations?

Thank you very much for the help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:40:57+00:00Added an answer on May 25, 2026 at 11:40 pm

    Another option is:

    1. Strip all characters you don’t care about from the string (punctuation, whitespace)
    2. Make it lowercase
    3. Sort the string
    4. Compare to the reference string (with .equals)

    I suspect your way is faster though.

    EDIT:

    Since @nibot disagrees with my even suggesting this, and I’m not one to argue back and forth without proof, here’s three solutions.

    They’re all implemented very similarly:

    1. Convert line to lowercase
    2. Ignore non-alphabetic characters
    3. ?
    4. Check of the result of 3. matches the result from the first line

    The ? part is one of:

    • Make a HashMap of character counts
    • Sorting the characters
    • Making a 26-int array (the ultimate hash table solution, but only works for the Latin alphabet)

    I ran them all with this:

    public static void time(String name, int repetitions, Function function,
            int expectedResult) throws Exception {
        long total = 0;
        for (int i = 0; i < repetitions; i++) {
            System.gc();
            long start = System.currentTimeMillis();
            int result = function.call();
            long end = System.currentTimeMillis();
            if (result != expectedResult) {
                System.out.println("Oops, " + name + " is broken");
                return;
            }
            total += end - start;
        }
        System.out.println("Executution of " + name + " took "
                + (total / repetitions) + " ms on average");
    }
    

    My file is similar to the one the OP posted, but made significantly longer, with a non-anagram about 20 lines from the end to ensure that the algorithms all work.

    I consistently get results like this:

    Execution of testWithHashMap took 158 ms on average
    Execution of testWithSorting took 76 ms on average
    Execution of testWithArray took 56 ms on average
    

    The HashMap one could be significantly improved if:

    • There was a way to make a HashMap<char, int>
    • There was a way to specify the default value of in a HashMap and a way to get-and-increment (so there would only be one lookup instead of 2)

    But, these aren’t in the standard library, so I’m ignoring them (just like most programmers using Java would).

    The moral of the story is that big O isn’t everything. You need to consider the overhead and the size of n. In this case, n is fairly small, and the overhead of a HashMap is significant. With longer lines, that would likely change, but unfortunately I don’t feel like figuring out where the break-even point is.

    And if you still don’t believe me, consider that GCC uses insertion sort in some cases in its C++ standard library.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given two different Microsoft Word document that my virus scanner has
I have been given a DLL (InfoLookup.dll) that internally allocates structures and returns pointers
I have been given a specification that requires the ISO 8601 date format, does
I use a cluster of about 30 machines that have all recently been reconfigured
I have been given a theme from a designer, and that theme has a
I've been trying to do something as a learning exercise that I thought should
I have been given task to check for url that site:domain (If index in
I have been given a set of historic data that requires line charting for
I have been given a task that can be simplified to this scenario: Customers
I have been given a textfile which contains data on time sheet. That line

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.