java - 使用 HashMap 比较两个字谜时出现编译错误

标签 java arrays string compiler-errors hashmap

我必须找到一种方法来检查两个字符串是否是字谜词。如果是,该方法应返回 true,否则返回 false。由于我自己无法想出正确的方法来完成此操作,所以我找到了 Rodney Shaghoulian 的另一段代码(Github:github.com/RodneyShag,HackerRank:hackerrank.com/RodneyShag),该代码应该可以工作:

import java.io.*;
import java.util.*;

public class Solution {

    static boolean isAnagram(String a, String b) {
        if (a == null || b == null || 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" );
    }
} 

我只是将其复制并粘贴到我自己的 IDE 中,但在第 18 行出现了三个编译错误:

map.merge(ch, 1, Integer::sum);

这么说

')' expected,   
illegal start of expression,   
error: ';' expected.

我不明白,因为我没有看到任何括号或缺少任何内容。该代码显然也对作者有用。 谁能帮我看看这个问题吗?

此外,有没有什么方法可以在不使用 HashMap 的情况下比较两个字符串以查看它们是否是字谜?可能使用 string to char[] 方法和 for 循环? (这是我最初想出的方法,我对 HashMap 一点也不熟悉。)

最佳答案

首先,这段代码没有问题,在我的环境中运行良好,你可以使用java -version检查你的JDK版本

这是我的 JDK 版本

$ 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表达式Integer::sum是JDK8及以后版本支持的功能,如果您使用的是1.8以下的JDK版本,则无法编译成功;

那么,我们来谈谈字谜:

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.

  1. 生成256位整数数组k
  2. 对于第一个字符串 sFirst 中的每个字符 x,x 的整数值为 y,将 k[y] 添加到 1
  3. 对于第二个字符串 sSecond 中的每个字符 x,x 的整数值为 y 减去 k[y] 1
  4. 如果数组 k 仍然全为零,则字符串 sFirst 和 sSecond 是 Anagrams

-

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;
    }
}

关于java - 使用 HashMap 比较两个字谜时出现编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52048811/

相关文章:

java - Hashtable 和 Collections.synchronizedMap(HashMap) 的区别

c++ - 将 c 数组分配给 C++ vector

c# - 如何按特殊顺序提取括号之间的文本?

java - 树状图排序

java - 调用 Thread.getAllStackTraces() 时发生挂起

python - 如何在 Python 的二维数组中查找值的索引?

arrays - 如何在 ruby​​ 数组中仅添加正数?

java - 通过网络从 C++ 客户端向 Java 服务器发送字符串

Python ljust 在包含 lin 时无法正确显示

java - 添加到链表前面