Let’s see program to check two strings are anagram or not in java.
import java.util.Arrays; public class StringAnagram { public static boolean angramCheck(String s1,String s2) { char[] c1 = s1.toCharArray(); char[] c2 = s2.toCharArray(); Arrays.sort(c1); Arrays.sort(c2); return Arrays.equals(c1, c2); } public static void main(String[] args) { System.out.println("Both string are angram "+angramCheck("ram", "mar")); } }
Output is :- Both string are angram true