Skip to main content

Anogram Java Program

package com.sbs.java8.praticse;

import java.util.Arrays;

public class AnagramClass {

public static void main(String[] args) {
String firstString = "1MASS1111124";
String secondString = "1SAMS1111124";
boolean status = false;

if (validateString(firstString,secondString)) {
char array1[] = firstString.toLowerCase().toCharArray();
char array2[] = secondString.toLowerCase().toCharArray();

Arrays.sort(array1);
Arrays.sort(array2);

status = Arrays.equals(array1, array2);
}
System.out.println("Anogram Status " + status);

}

private static boolean validateString(String string,String string2) {
if (string == null || string.isEmpty()) {
return false;
}
if (string2 == null || string2.isEmpty()) {
return false;
}
if(string.length() != string2.length()){
return false;
}

return true;
}
}

Comments

Popular posts from this blog

Compress a String

package com.sbs.java8.praticse; public class StringCompression { public StringCompression() { // TODO Auto-generated constructor stub } public static void main(String[] args) { System.out.println(compressString("aaaabbbbbccccAAAAccccccccdefg")); } public static String compressString(String str) { //String str = "aaaabbbbbcccc"; char[] charArray = str.toCharArray(); String compressedString = ""; int i = 0; while (i charArray.length)? str: compressedString; return output; } }

String Pool Vs String Definition (Using 'new' Operator)

Strings can be defined in Java in two ways. 1. String Literals 2  Traditional way(using 'new' operator) String Literals If a String is created by using String literal notation, memory will be allocated directly in string pool. String pool is subset of Heap memory (Where objects will be created). e.g. String companyName = "Surimenus";      String empName= "Bhargav";       Using 'New' Operator If a String is created using new operator, memory will be allocated in Heap Memory not in String pool. e.g. String companyName = new String("Surimenus"); String empName= new String("Bhargav"); String Pool vs Using 'new' Operator String which are created in String pool will re-reference by reference which contains the same content. For example consider the following. String cn1 = "Surimenu"; String cn2 = "Surimenu"; In the above scenario cn1 and cn2 references having the same content and these are created in Strin...