Note that there are some explanatory texts on larger screens.

plurals
  1. POArray of Strings contains only anagrams?
    text
    copied!<p>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.</p> <p>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:</p> <p>A Cab Deed Huffiest Minnows Loll<br> A Cab Deed Huffiest Minnow Lolls<br> A Cab Deed Shuffles Million Wont<br> A Cab Deed Shuffles Million Town </p> <p>The program should return True. For input:</p> <p>A Cab Deed Huffiest Minnows Loll<br> A Cab Deed Huffiest Minnow Lolls hi<br> A Cab Deed Shuffles Million Wont<br> A Cab Deed Shuffles Million Town</p> <p>the output will have to be False (because of the second line, of course).</p> <p>Now, what I thought is pretty straightforward:</p> <ul> <li>I create 2 HashMap: ref and cur.</li> <li>I parse the first line of the text, filling ref. I will count only alphabetical letters.</li> <li>for each other line, I parse the line into cur and check if cur.equals(ref): if so return false</li> <li>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.</li> </ul> <p>And...this would be it. I tried it with an input text of 88000 lines, and it works pretty fast.</p> <p>Any comments? Suggestions? Optimizations?</p> <p>Thank you very much for the help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload