Reputation: 1
In the for loop i want to compare letter by letter in the first word to all of the letters in the second word. I thought about putting it into alphabetical order but i think that's against the rules.
import java.util.*;
public class Anagrams{
public static void main (String [ ] arg){
Scanner Scan = new Scanner (System.in);
//ask for thingss...am i allowed to use scanner?
System.out.println("enter the first word");
String word1 = Scan.nextLine().toLowerCase();
System.out .println("enter the second word");
String word2 = Scan.nextLine().toLowerCase();
//make into string builder
StringBuilder ThisWord = new StringBuilder ("");
ThisWord.append(word1);
StringBuilder ThatWord = new StringBuilder ("");
ThatWord.append(word2);
//do the booleanssss
boolean lnth = true;
boolean found = false;
//see if they the same length
if(ThisWord.length() != ThatWord.length())
{
lnth = false;
System.out.println("sorry sis, "+ThatWord+" and "+ThisWord+" not the same length");
}
else
{
outerloop:
for (int i = 0; i < ThisWord.length(); i++)
{
for (int j = 0; i < ThatWord.length(); i++)
{
found = false;
if(ThisWord.charAt(i) == ThatWord){
ThatWord.deleteCharAt(j);
found = true;
break;
}
else{
System.out.println(ThisWord+" and "+ThatWord+" not anagrams - letters do not match");
found = false;
break outerloop;
}
}
}
}
Upvotes: 0
Views: 587
Reputation: 61
HashMap<Character, Integer> alphabet = new HashMap<Character, Integer>();
//assign prime numbers to alphabet
alphabet.put('a', 1);
alphabet.put('b', 2);
alphabet.put('c', 3);
alphabet.put('d', 5);
alphabet.put('e', 7);
alphabet.put('f', 11);
alphabet.put('g', 13);
alphabet.put('h', 17);
alphabet.put('i', 19);
alphabet.put('j', 23);
alphabet.put('k', 29);
alphabet.put('l', 31);
alphabet.put('m', 37);
alphabet.put('n', 41);
alphabet.put('o', 43);
alphabet.put('p', 47);
alphabet.put('q', 53);
alphabet.put('r', 59);
alphabet.put('s', 61);
alphabet.put('t', 67);
alphabet.put('u', 71);
alphabet.put('v', 73);
alphabet.put('w', 79);
alphabet.put('x', 83);
alphabet.put('y', 89);
alphabet.put('z', 97);
public static boolean anagram(String str1, String str2) {
if(str1.length() != str2.length())
return false;
int str1product = 1;
int str2product = 1;
for(int i = 0; i < str1.length(); i++) {
str1product *= alphabet.get(str1.charAt(i));
str2product *= alphabet.get(str2.charAt(i));
}
return (str1product == str2product);
}
Upvotes: 1
Reputation: 276
Can we use something like this:
public class Anagrams
{
public static void main(String[] args)
{
System.out.println(checkAnagram("abc","cba"));
System.out.println(checkAnagram("abca","cba"));
System.out.println(checkAnagram("abd","cba"));
System.out.println(checkAnagram("otp","pot"));
System.out.println(checkAnagram("poo","oop"));
}
private static boolean checkAnagram(String first, String second)
{
if(first.length()!=second.length())
return false;
int [] alphabet = new int [26];
for(int i= 0;i< first.length();i++ )
{
alphabet[first.charAt( i ) - 97]++;
alphabet[second.charAt( i ) - 97]++;
}
for(int i =0 ;i< alphabet.length;i++)
{
if(alphabet[i] % 2!=0)
{
return false;
}
}
return true;
}
}
Upvotes: 2
Reputation: 1216
One solution might be to assign prime numbers to the letters of the alphabet (let's say these, where the first column should be the characters in the alphabet and the other are the primes obviously). Put these in a HashMap<Character, Integer>
. Let's call it PRIME_MAP
. It should look like a -> 2, b -> 3, c -> 5, ...
You create 2 for
cycles. In each, you iterate through ThisWord
and ThatWord
respectively.
You create a product from the letters of the words like so:
int thisWordProduct = 1;
for(int i = 0; i < ThisWord.length(); i++) {
thisWordProcuct *= PRIME_MAP.get(thisWordProduct.charAt(i);
}
And similarly for the other.
If the 2 products are equal, the 2 words are anagrams.
This solution might be a bit more abstract then the others suggested, but this should be more optimal then nested loops
Upvotes: 1
Reputation: 868
Few methods you can consider Method 1 - Sorting the strings via the alphabet and then comparing them.
Method 2- Creating a helper array. For each character in the first string you would increment the value in the array[charValue] by 1. For each character in the second string you would decrements the value in the array[charValue]. If it is an anagram you would get an array that will be filled with zeros at the end.
Method 3- iterating characters of the first string and removing the character from the second string when found. If str2.isEmpty () -> Anagram.
Upvotes: 1