📌  相关文章
📜  在Java中使用HashMap检查两个字符串是否是彼此的Anagram

📅  最后修改于: 2022-05-13 01:55:01.068000             🧑  作者: Mango

在Java中使用HashMap检查两个字符串是否是彼此的Anagram

编写一个函数来检查两个给定的字符串是否是彼此的Anagram

一个字符串的变位词是另一个包含相同字符的字符串,只是字符的顺序可以不同。

检查两个字符串是否是彼此的字谜

方法:哈希图也可用于查找任何两个给定字符串是否是字谜,通过将每个字符串的字符映射到单独的哈希图并将它们一起比较。

执行:

// Java code to check whether two strings
// are Anagram or not using HashMap
  
import java.io.*;
import java.util.*;
  
class GFG {
  
    // Function to check whether two strings
    // are an anagram of each other
    static boolean areAnagram(String str1, String str2)
    {
  
        HashMap hmap1
            = new HashMap();
        HashMap hmap2
            = new HashMap();
  
        char arr1[] = str1.toCharArray();
        char arr2[] = str2.toCharArray();
  
        // Mapping first string
        for (int i = 0; i < arr1.length; i++) {
  
            if (hmap1.get(arr1[i]) == null) {
  
                hmap1.put(arr1[i], 1);
            }
            else {
                Integer c = (int)hmap1.get(arr1[i]);
                hmap1.put(arr1[i], ++c);
            }
        }
  
        // Mapping second String
        for (int j = 0; j < arr2.length; j++) {
  
            if (hmap2.get(arr2[j]) == null)
                hmap2.put(arr2[j], 1);
            else {
  
                Integer d = (int)hmap2.get(arr2[j]);
                hmap2.put(arr2[j], ++d);
            }
        }
  
        if (hmap1.equals(hmap2))
            return true;
        else
            return false;
    }
  
    // Test function
    public static void test(String str1, String str2)
    {
  
        System.out.println("Strings to be checked are:\n"
                           + str1 + "\n" + str2 + "\n");
  
        // Find the result
        if (areAnagram(str1, str2))
            System.out.println("The two strings are "
                               + "anagrams of each other\n");
        else
            System.out.println("The two strings are not"
                               + " anagrams of each other\n");
    }
  
    // Driver program
    public static void main(String args[])
    {
  
        // Get the Strings
        String str1 = "geeksforgeeks";
        String str2 = "forgeeksgeeks";
  
        // Test the Strings
        test(str1, str2);
  
        // Get the Strings
        str1 = "geeksforgeeks";
        str2 = "geeks";
  
        // Test the Strings
        test(str1, str2);
    }
}
输出:
Strings to be checked are:
geeksforgeeks
forgeeksgeeks

The two strings are anagrams of each other

Strings to be checked are:
geeksforgeeks
geeks

The two strings are not anagram of each other

相关文章:检查两个字符串是否互为变位词