Skip to content

Instantly share code, notes, and snippets.

@ayyucedemirbas
Created September 20, 2018 11:48
Show Gist options
  • Save ayyucedemirbas/78c4cf71f21ec51798884f76be802e5d to your computer and use it in GitHub Desktop.
Save ayyucedemirbas/78c4cf71f21ec51798884f76be802e5d to your computer and use it in GitHub Desktop.
Check for Anagram
package anagram;
import java.util.Arrays;
public class checkForAnagram {
public static boolean _checkForAnagram(String first, String second ) {
int size1= first.length(), size2=second.length(), count=0;
char[] f=new char[size1];
char[] s= new char[size2];
for(int i=0; i<size1; i++) {
if(first.charAt(i)!= ' ') {
f[count]=first.charAt(i);
count++;
}
}
count=0;
for(int i=0; i<size2; i++) {
if(second.charAt(i)!= ' ') {
s[count]=second.charAt(i);
count++;
}
}
for(int i=0; i<size1-1;i++) {
for(int j=0; j<size1-1;j++) {
if(f[j]> f[j+1]) {
int temp= f[j+1];
f[j+1]=f[j];
f[j]=(char)temp;
}
}
}
for(int i=0; i<size2-1;i++) {
for(int j=0; j<size2-1;j++) {
if(s[j]> s[j+1]) {
int temp= s[j+1];
s[j+1]=s[j];
s[j]=(char)temp;
}
}
}
if(Arrays.equals(f, s)) {
return true;
}
return false;
}
public static void main(String[] args) {
String s="listen", s1="silent";
if(_checkForAnagram(s,s1)) {
System.out.println("Anagram");
}
else System.out.println("Not anagram");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment