Table Of Contents
What is String Anagram?
Ans: When two String have same character/letter in them, but there order or sequence can be different such strings are called as String Anagram.
For Example:- String 1- Night String-2 Thing Above Two String are Anagram as after rearranging characters we get either, Night or Thing or vice versa. Hence they are Anagram.
Explanation
- The Logic For Checking Whether Two String are Anagram or not is very simple.
- First compare the length of both String if they are not equal, directly display Strings are not Anagram.
- Now if length of two Strings are equal then, sort the string in alphabetical order then compare them.
- If result comes equal display Strings are Anagram
- Else Display Strings are not Anagram.
Algorithm
- Start
- Take two String inputs
- String s1,s2
- Convert them into Lowecase
- s1=s1.toLowerCase
- s2=s2.toLowerCase
- Check for condition
- if(s1.length()!=s2.length())
- Print Strings are not Anagram
- Else
- Convert them into Character Array
- char[] a1 = s1.toCharArray();
- char[] a2 = s2.toCharArray();
- sort them using sort method
- Arrays.sort(a1)
- Check for condition
- if(Arrays.equals(a1,a2)==true)
- print String are Anagram
- Else print they are not Anagram
- if(s1.length()!=s2.length())
- End
Program
/*
Java Program To Check Whether Two String are Anagram or Not? */
import java.util.Scanner;
import java.util.Arrays;
public class StringAnagram {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter String-1");
String s1=sc.nextLine();
System.out.println("Enter String-2");
String s2=sc.nextLine();
s1= s1.toLowerCase();
s2=s2.toLowerCase();
if(s1.length()!=s2.length()){
System.out.println("Strings are Not Anagram");
}
else {
char[] a1 = s1.toCharArray();
char[] a2 = s2.toCharArray();
Arrays.sort(a1);
Arrays.sort(a2);
if(Arrays.equals(a1,a2)==true){
System.out.println("String are Anagram");
}
else
System.out.println("String are not Anagram");
}
}}
Output 1:
Enter String-1
Night
Enter String-2
Thing
String are Anagram
Output 2:
Enter String-1
Noteshacker
Enter Strnig-2
hackerpedia
String are not Anagram
Related Topics
- Arithmetic Operations Java Program
- Java program to perform Logical and Relational operations
- Java program to Implement Mathematical functions defined in Java Math class
- Java basic Programs with Explanation
- Java Program to find Min,Max Element in an Array
- Java Program to sort Array Elements in ascending and descending order
- Java Program to display Array item at Even & Odd position
- Program to Calculate Sum and Average of Array elements
- Java Program to find Index of specific Element in an array
- Program to find common elements between two arrays
- Java Program to Add/Insert specific Element to an Array
- Java Program to delete specific element from an Array
- Program to calculate frequency of each element in Array
- Java Program to find Even and Odd Numbers in the arrays
- Java Program to search Given Number in an Array
- Java Program to Add or Sub Two Matrices
- Java Program to Multiply two or More Matrices
- Java Program to Print Array in Reverse Order
- Java Program To Transpose Given Matrix
- Java Programs List- Java Programs Examples With Output