Java Program To Check two strings are Anagram or not [pdf]

the schoolprogrammer

Two strings A and B are called anagrams if they contain all the same characters in the same frequencies. For example, the anagrams of CAT are CATACTTACTCAATC, and CTA.

Input Format:

The first line contains a string denoting A .
The second line contains a string denoting B.

Constraints:

  • Strings A and B consist of English alphabetic characters.
  • The comparison should NOT be case sensitive.

Output Format:

Print "Anagrams" if  A and  B are case-insensitive anagrams of each other; otherwise, print "Not Anagrams" instead.

Sample Input 1

anagram 
margana

Sample Output 1

Anagrams

Explanation:

CharacterFrequency: anagramFrequency: margana
A or a33
G or g11
N or n11
M or m11
R or r11

Sample Input 2

anagramm
marganaa

Sample Output 2

Not Anagrams

Explanation:

CharacterFrequency: anagrammFrequency: marganaa
A or a34
G or g11
N or n11
M or m21
R or r11

The two strings don't contain the same number of a's and m's, so we print "Not Anagrams".



Join Us

For those who prefer reading on mobile, we have Telegram channel and WhatsApp group that will allow you to receive updates, announcements, and links to our stories straight to your mobile device..



Sample Input 3

Hello
hello

Sample Output 3

Anagrams

Explanation :

CharacterFrequency: HelloFrequency: hello
E or e11
H or h11
L or l22
O or o11

The two strings contain all the same letters in the same frequencies, so we print "Anagrams".

Steps to be followed:

  1. Take two Strings A and B as input.
  2. Check length of both strings if not same then print Not anagram.
  3. If length is same then create a flag variable 'k' .
  4. Initialize 'k' to 0.
  5. Create a loop i=0 - length of B.
  6. For each value of i, find index of A.char(i) in B.
  7. If a==-1 i.e not found set k=1 and break.
  8. Else change A=A.substring(0,n)+A.substring(n+1)
  9. Then print result according to value of 'k'.

Code:

/**
 * Java Program to check whether two strings are anagram or not.
 *
 * @author (THESCHOOLPROGRAMMER)
 * www.theschoolprogrammer.com
 */
import java.util.*;
public class isAnagram
{
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        String A=sc.nextLine();
        String B=sc.nextLine();
        A=A.toUpperCase();
        B=B.toUpperCase();
        int k=0;
        if(A.length()==B.length())
        {
            for( int i=0;i<B.length();i++)
            {
                int n=A.indexOf(B.charAt(i));
                if(n==-1)
                    {
                      k=1;
                      break;
                    }
                else
                    A=A.substring(0,n)+A.substring(n+1);
            }
            System.out.println(k==0?"Anagrams":"Not Anagrams");
        }
        else
            System.out.println("Not Anagrams");
    }
}

Found this useful? Share with your friends. Share



Also check:





Comments

  1. If you are interested in python code of this program, here it is
    .
    .
    # function to check if two strings are
    # anagram or not
    def check(s1, s2):

    # the sorted strings are checked
    if(sorted(s1)== sorted(s2)):
    print("The strings are anagrams.")
    else:
    print("The strings aren't anagrams.")

    # driver code
    s1 ="listen"
    s2 ="silent"
    check(s1, s2)

    thank me later. ��

    ReplyDelete
You are welcome to share your ideas with us in comments!!!

Full Screen Mode

Archive

Contact Form

Send