Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

anagram java program

import java.util.Arrays;  
   
public class AnagramString {  
    static void isAnagram(String str1, String str2) {  
        String s1 = str1.replaceAll("s", "");  
        String s2 = str2.replaceAll("s", "");  
        boolean status = true;  
        if (s1.length() != s2.length()) {  
            status = false;  
        } else {  
            char[] ArrayS1 = s1.toLowerCase().toCharArray();  
            char[] ArrayS2 = s2.toLowerCase().toCharArray();  
            Arrays.sort(ArrayS1);  
            Arrays.sort(ArrayS2);  
            status = Arrays.equals(ArrayS1, ArrayS2);  
        }  
        if (status) {  
            System.out.println(s1 + " and " + s2 + " are anagrams");  
        } else {  
            System.out.println(s1 + " and " + s2 + " are not anagrams");  
        }  
    }  
   
    public static void main(String[] args) {  
        isAnagram("Keep", "Peek");  
        isAnagram("Mother In Law", "Hitler Woman");  
    }  
}  
Comment

anagram java

public class Anagram {
	
	public static void main(String[] args) {
		
		System.out.println("is Anagram >>>>" + isAnagram("aabs", "baas"));
	}

	private static boolean isAnagram(String s1, String s2) {
		
		int[] count = new int[256];
		
		boolean result = true;
		
		for (int i = 0; i < s1.length(); i++) {
			
			count[s1.charAt(i)]++;
			count[s2.charAt(i)]--;
		}
		
		for (int i = 0; i < count.length; i++) {
			
			if(count[i] !=0) {
				result =false;
			}
			
		}
		return result;
		
	}

}
Comment

PREVIOUS NEXT
Code Example
Java :: arraylist string to string array 
Java :: convert ascii to string java 
Java :: How to compare lists of custom classes without defining equals() and hashCode()? 
Java :: java load .json file 
Java :: find shortest string in array java 
Java :: difference between abstract class and final 
Java :: java int to int array 
Java :: object to array java 
Java :: how to convert outputstream to bytearrayoutputstream in java 
Java :: read csv java android 
Java :: how to initialize an array in java 
Java :: java string builder 
Java :: mongoose collection name 
Java :: create file from byte array java 
Java :: java swing alert window 
Java :: java foreach list 
Java :: how to add minutes in date in java 
Java :: java stream order by property 
Java :: convert int to double with 2 decimal places java 
Java :: isinstance java 
Java :: recyclerview stop scrolling 
Java :: how to make an int into a string java 
Java :: event api spigot 
Java :: How to perform breadth first search through a binary tree, in Java? 
Java :: java print hex format 
Java :: how to enable cors request on springboot application 
Java :: how to print multiple things in one line java 
Java :: java string format thousand separator 
Java :: How to solve the N queens problem in Java? 
Java :: how to declare an array in java 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =