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]

Advantage and disadvantage of datastructures

Characteristics of Data Structures Data Structure Advantages Disadvantages Array Quick inserts Fast access if index known< Slow search Slow deletes Fixed size Ordered Array Faster search than unsorted array Slow inserts Slow deletes Fixed size Stack Last-in, first-out acces Slow access to other items Queue First-in, first-out access Slow access to other items Linked List Quick inserts Quick deletes Slow search Binary Tree Quick search Quick inserts Quick deletes (If the tree remains balanced) [Read More]

Compare two string literals

public boolean compare(String text1, String text2){  
if(text1\=\="" && text2!\="")  
   text1\=null;  
if(text2\=\=""&&text1!\="")  
   text2 \= null;  
if (text1\=\= null && text2\=\=null)   
         return true;  
if (text1.length()\=\=0 && text2.length()\=\=0)   
         return true;  
if (text1.length()\=\=0 || text2.length()\=\=0)   
         return false;  
if (text2\=\=null || text2\=\=null)   
        return false;  
  
      return text1.compareTo(text2);  
  
  
}