Posts

Showing posts with the label Anagram

Java program to check for Anagram

An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other. We are given two strings, we need to check if strings are Anagram. Examples: Input : "anagram", "nagaram" Output : yes Input : "mat", "fat" Output : no Method 1: Check if Two Strings Are Anagram using Array import java.util.Arrays ; public class Main { public static boolean checkAnagram ( String s1, String s2) { // Remove all the white space s1 = s1.replaceAll( " \\ s " , "" ); s2 = s2.replaceAll( " \\ s " , "" ); // Check if both length matches if (s1.length() != s2.length()) return false ; else { // Convert both Strings into lower case and into Character Array char [] arr1 = s1.toLowerCase().toCharArray(); char []...