Compilation error when comparing two anagrams while using a hashmap
Compilation error when comparing two anagrams while using a hashmap
I have to find a way to check two strings if they are anagrams. If they are, the method should return true and false otherwise. Since I couldn't come up with a proper way to do it on my own I found another piece of code by Rodney Shaghoulian (Github: github.com/RodneyShag, HackerRank: hackerrank.com/RodneyShag) that is supposed to work:
import java.io.*;
import java.util.*;
public class Solution
static boolean isAnagram(String a, String b) a.length() != b.length())
return false;
a = a.toLowerCase();
b = b.toLowerCase();
HashMap<Character, Integer> map = new HashMap<>();
/* Fill HashMap with 1st String */
for (int i = 0; i < a.length(); i++)
char ch = a.charAt(i);
map.merge(ch, 1, Integer::sum);
/* Compare 2nd String to 1st String's HashMap */
for (int i = 0; i < b.length(); i++)
char ch = b.charAt(i);
if (map.containsKey(ch) && map.get(ch) > 0)
map.put(ch, map.get(ch) - 1);
else
return false;
return true;
public static void main(String args)
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
I simply copied and pasted it into my own IDE but got three compilation errors on line 18:
map.merge(ch, 1, Integer::sum);
Saying that
')' expected,
illegal start of expression,
error: ';' expected.
Which I don't understand since I don't see any parentheses or anything missing. The code also obviously worked for the author, too.
Can anyone help me see the problem?
Also, is there any way to compare two strings to see if they're anagrams without using a hashmap? Possibly using the string to char method and for loops? (This was the original way I came up with, I am not at all familiar with hashmaps.)
When I run this on my IDE it compiles just fine
– GBlodgett
Aug 28 at 1:29
This is super weird, I literally copied and pasted that code multiple times and I still get that error... Thank you for your ideas.
– jo7645
Aug 28 at 1:44
It sounds like you have a stray
}//(/) in your program.– GBlodgett
Aug 28 at 1:46
{
(
)
you are using method reference
Integer::sum which was introduced in java 8.. are you using an older version?– Kartik
Aug 28 at 1:59
Integer::sum
1 Answer
1
First of all, this code is no problem and runs well in my environment, and you can check your JDK version using java -version
java -version
Here's my JDK version
$ java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
Lambda expressions Integer::sum are features supported by JDK8 and later versions, if you are using a JDK version under 1.8, you cannot compile successfully;
Integer::sum
then, let's talk about the anagrams:
We can use an array, which is the index of the character, and then count the number of occurrences of each character in the string. In the first string, each character that appears is added to the corresponding array position. In the second string, each character that appears is subtracted one from the corresponding position of the array. So what we do is we go through the first string, we go through the second string, and the only way that both of these strings are going to be the Anagrams is if this array is still going to be all 0. This means that one of the two strings has the same number of characters.
-
public class CustomStringUtil
public static boolean secondIsAnagram(String sFirst, String sSecond)
if (sFirst.length() != sSecond.length())
return false;
int asciiChars = new int[256];
for (int i = sFirst.length() - 1; i >= 0; --i)
++asciiChars[sFirst.charAt(i)];
for (int i = sFirst.length() - 1; i >= 0; --i)
char currChar = sSecond.charAt(i);
if (asciiChars[currChar] == 0)
return false;
--asciiChars[currChar];
return true;
Sorry for my noob question but why did you choose to a 256-integer array? I mean how did you come up with the numer 256?
– jo7645
Aug 28 at 2:15
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
One alternative approach I might use is to iterate over every letter in the word, check if the second word contains the letter. If it does remove it from the second word. If it doesn't return false.
– GBlodgett
Aug 28 at 1:27