Java Program To Check two strings are Anagram or not [pdf]
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 CAT
, ACT
, TAC
, TCA
, ATC
, 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:
Character | Frequency: anagram | Frequency: margana |
---|---|---|
A or a | 3 | 3 |
G or g | 1 | 1 |
N or n | 1 | 1 |
M or m | 1 | 1 |
R or r | 1 | 1 |
Sample Input 2
anagramm marganaa
Sample Output 2
Not Anagrams
Explanation:
Character | Frequency: anagramm | Frequency: marganaa |
---|---|---|
A or a | 3 | 4 |
G or g | 1 | 1 |
N or n | 1 | 1 |
M or m | 2 | 1 |
R or r | 1 | 1 |
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 :
Character | Frequency: Hello | Frequency: hello |
---|---|---|
E or e | 1 | 1 |
H or h | 1 | 1 |
L or l | 2 | 2 |
O or o | 1 | 1 |
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Steps to be followed:
- Take two Strings A and B as input.
- Check length of both strings if not same then print Not anagram.
- If length is same then create a flag variable 'k' .
- Initialize 'k' to 0.
- Create a loop i=0 - length of B.
- For each value of i, find index of A.char(i) in B.
- If a==-1 i.e not found set k=1 and break.
- Else change A=A.substring(0,n)+A.substring(n+1)
- 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:
- JAVA Program To Check Given Number Is Prime-Adam or Not [ISC 2020 Practical]
- ISC 2020 Computer Science Practical Paper solved
- Program to sort elements in ascending order using the bubble sort technique [ICSE 2019]
- AVA Program to print common factors of two number.
- JAVA Program to find LCM of two numbers using simplest algorithm
- JAVA Program to find the largest factor of a number
- Java Program to accept three numbers and print the second lowest number.
If you are interested in python code of this program, here it is
ReplyDelete.
.
# 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. ��
Thank You ☺
Delete