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 :: junit check class type 
Java :: java print method 
Java :: Hash map, Hash set and Hash Table difference 
Java :: java get enum by ordinal 
Java :: spigot plugin send message 
Java :: remove all element in a list java 
Java :: android view set padding programmatically 
Java :: parcourir un string java 
Java :: spring security after login redirect 
Java :: Java LogManager 
Java :: final vs static keyword in java 
Java :: java check if string ends with 
Java :: return two values in java 
Java :: call function after specific time java android 
Java :: how to add arms to armor stands 1.16 Java Edition 
Java :: Android Number Picker format JAVA 
Java :: new thrad java 
Java :: hasNext for scanner class java 
Java :: mvn spring boot ends when closing vm 
Java :: get current background color of TextView java 
Java :: how to convert integer to string in java 
Java :: Java How to use NavigableSet? 
Java :: android java show hide keyboard in AndroidManifest 
Java :: java bogo sort 
Java :: split on . in java 
Java :: how to make jenkins pipeline choose specific java version 
Java :: how to create dynamic string array in java 
Java :: take a peice from array java 
Java :: java enum to string 
Java :: new in t arrray java 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =