Java examples for java.lang:String Compare
This method returns true if the two input strings (ignoring case) are anagrams of each other, otherwise returns false.
import java.io.File; import java.io.FileNotFoundException; import java.util.Comparator; import java.util.Scanner; public class Main{ public static void main(String[] argv) throws Exception{ String s1 = "java2s.com"; String s2 = "java2s.com"; System.out.println(areAnagrams(s1,s2)); }//from w w w . j a v a 2 s . c o m private static SortMethod method = SortMethod.INSERTION; /** * This method returns true if the two input strings (ignoring case) are anagrams of each other, otherwise returns false. * @param s1 * @param s2 * @return boolean */ public static boolean areAnagrams(String s1, String s2) { if (s1 == null || s2 == null) { throw new IllegalArgumentException( "Cannot compare a null string!"); } //Sort each string ignoring case s1 = sort(s1.toLowerCase()); s2 = sort(s2.toLowerCase()); //Compare and return results if (s1.length() != s2.length()) return false; if (s1.compareTo(s2) != 0) return false; else return true; } /** * This method returns the sorted version of the input string. * The sorting must be accomplished using either an insertion sort or a selection sort, * depending on the method state variable. This method should preserve case. * So the result of calling AnagramUtil.sort("Stephen") should be "Seehnpt". * @param s * @return */ public static String sort(String s) { if (s == null) { throw new IllegalArgumentException("String cannot be null!"); } //Covert String s to character array Character[] arr = stringToCharacterArray(s); //Create String Comparator to pass the sorting method Comparator<Character> stringComparator = new NaturalComparator<Character>(); //Choose the sorting method if (method == SortMethod.INSERTION) { insertionSort(arr, stringComparator); } else { selectionSort(arr, stringComparator); } //Return sorted String return characterArrayToString(arr); } /** * This method sorts the given strings into groups of anagrams * @param sarr * @return */ public static String[] sort(String[] sarr) { // Create String Comparator to pass the sorting method Comparator<String> anagramComparator = new AnagramComparator(); // Choose the sorting method if (method == SortMethod.INSERTION) { insertionSort(sarr, anagramComparator); } else { selectionSort(sarr, anagramComparator); } // Return sorted Strings return sarr; } private static Character[] stringToCharacterArray(String s) { char[] arr = s.toCharArray(); Character[] carr = new Character[arr.length]; for (int i = 0; i < arr.length; i++) { carr[i] = arr[i]; } return carr; } /** * This generic method sorts the input array using an insertion sort and the input Comparator object. * @param arr * @param c */ public static <T> void insertionSort(T[] arr, Comparator<? super T> c) { // validate parameters if (arr == null) { throw new IllegalArgumentException( "The given array cannot be null!"); } if (c == null) { throw new IllegalArgumentException( "The given comparator cannot be null!"); } T key; // The item to be inserted int gapIndex; // Keeps track of the current gap index // Go through each item for (int i = 1; i < arr.length; i++) { // Make the gap key = arr[i]; gapIndex = i; // Position the gap while (gapIndex > 0 && c.compare(arr[gapIndex - 1], key) > 0) { // Move the gap up arr[gapIndex] = arr[gapIndex - 1]; gapIndex--; } // Insert into the gap arr[gapIndex] = key; } } /** * This generic method sorts the input array using a selection sort and the input Comparator object. * @param arr * @param c */ public static <T> void selectionSort(T[] arr, Comparator<? super T> c) { // validate parameters if (arr == null) { throw new IllegalArgumentException( "The given array cannot be null!"); } if (c == null) { throw new IllegalArgumentException( "The given comparator cannot be null!"); } int smallestIndex; for (int i = 0; i < arr.length - 1; i++) { smallestIndex = i; // Find the index of the smallest element for (int j = i + 1; j < arr.length; j++) { if (c.compare(arr[j], arr[smallestIndex]) < 0) { smallestIndex = j; } } // Swap elements if necessary if (smallestIndex != i) { T temp = arr[i]; arr[i] = arr[smallestIndex]; arr[smallestIndex] = temp; } } } private static String characterArrayToString(Character[] carr) { StringBuilder result = new StringBuilder(); for (Character each : carr) { result.append(each); } return result.toString(); } }