Sort an array of strings so that anagrams are next to each other
Problem
Write a method to sort an array of strings so that all the anagrams are next to each other.
Example
INPUT : "xyz", "ca", "ab", "ac", "ba", "zyx" OUTPUT: "ab", "ba", "ac", "ca", "xyz", "zyx" Lets see the solutions now.
Solution
Method 1 - Using bubble sort
Check if two pairs of strings are anagrams or not, if yes, swap.
Java code
private static boolean areAnagrams(String s1, String s2) { if (s1.
[Read More]