String Anagram Code in Java

String Anagram Code | Java


A string anagram is a word or phrase formed by rearranging the letters of another word or phrase, typically using all the original letters exactly once. For example, "listen" and "silent" are anagrams of each other.

import java.util.*;

class HelloWorld {

    public static void main(String[] args) {

        String s1 = "asgar";

        String s2 = "agsag";

        if(s1.length() != s2.length()){

          System.out.println("not Anagram");

        }

char a[]= s1.toCharArray();

char b[] = s2.toCharArray();

Arrays.sort(a);

Arrays.sort(b);

boolean ismatch = Arrays.equals(a,b);

  if(ismatch){

      System.out.println(" Anagram");

   }else{

       System.out.println("not Anagram");

    }}};


output:

not Anagram

JAVA

Recursion in java

Substring in java

Previous Post Next Post

Contact Form