A String is Anagram or not in java


import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
boolean isAnagram=false;
Scanner sc = new Scanner(System.in);
String str1=sc.nextLine();
String str2=sc.nextLine();
boolean[] visited = new boolean[str2.length()];
if(str1.length()==str2.length())
{
for(int i=0;i<str1.length();i++){
char c = str1.charAt(i);
isAnagram=false;
for(int j=0;j<str2.length();j++){
if(str2.charAt(j)==c && !visited[j]){
visited[j]=true;
isAnagram=true;
break;
}
}
if(!isAnagram)
break;
}
}
if(!isAnagram)
System.out.println("Not Anagram");
else
System.out.println("Anagram");
}
}

Comments

Popular posts from this blog

Basic calculator using C programming

Factorial of consecutively 5 numbers(input)

Square Root of a number without using math.h function