2
I’m solving a problem where I get one array
of strings with different words, I have to find out for each word (string) which are your friends. Friendly words are those that have the same letters in different or equal order.
The problem is I have to return one array
where each position has the words "friends".
Example:
Input: {"ola", "lao", "aol", "asd", "asdf", "fdsa"}
Output: {"ola lao aol", "asdf fdsa"}
To learn how you are friends I created the following method:
static boolean sameChars( String firstStr , String secondStr ) {
char[ ] first = firstStr.toCharArray( );
char[ ] second = secondStr.toCharArray( );
Arrays.sort( first );
Arrays.sort( second );
return Arrays.equals( first , second );
}
My idea was to walk the array
and go building the array
final, I’m just not getting a minimally efficient logic.
Any hint that might help me come up with a solution?
Any questions just ask. I hope I’ve helped!
– Sérgio Mucciaccia
Thanks for the help, I realized the logic missed only the part where words that have no friends should not enter the output array.
– user2989745
Oh yes, in your example "Asd" did not appear in the output. Nor had I seen it! Okay, I was going to modify the answer, but since you already posted, it’s a good thing there are two alternatives. If you prefer I upgrade mine to be right.
– Sérgio Mucciaccia
It is important to accept one of the answers, because then people can see that the question has already been answered! This is good both for those who consult the question later and for those who are looking for questions to answer. You can do this by clicking the right one below the answer score. More information here: http://meta.pt.stackoverflow.com/questions/1078/como-e-por-que-aceitar-uma-reply
– Sérgio Mucciaccia