I'm solving a problem where I get a 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 that I have to return a array
where each position has the words "friends".
Example:
Input : {"ola", "lao", "aol", "asd", "asdf", "fdsa"}
Output : {"ola lao aol", "asdf fdsa"}
To find out 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 go through the initial array
and build the final array
, I'm just not achieving a minimally efficient logic.
Any tips that can help me get the solution?