Skip to content

Added Anagram problem #249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions anagram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.*;

class anagram {
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();

if (s1.length() != s2.length()) {
System.out.println("Not a anagram");
} else {
boolean isanagram = false;
char visited[] = new char[s1.length()];
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
isanagram = false;
for (int j = 0; j < s2.length(); j++) {
if (ch == s2.charAt(j) && visited[j] != ch) {
isanagram = true;
visited[i] = ch;
break;
}
}
if (isanagram == false) {
System.out.println("not a anagram");
break;
}
}
if (isanagram) {
System.out.println("ANAGRAM");
}

}

int fors1[] = new int[256];
int fors2[] = new int[256];
for (int i = 0; i < s1.length(); i++) {
char ch = s1.charAt(i);
int index = (int) ch;
fors1[index]++;

ch = s2.charAt(i);
index = (int) ch;
fors2[index]++;

}
boolean isana = true;
for(int k=0 ; k<256; k++)
{
if(fors1[k]!=fors2[k])
{
System.out.println("not an anagram");
isana = false;
break;
}
}
if(isana)
{
System.out.println("yes this is an anagram ");
}

}
}