📌  相关文章
📜  Java程序检查两个字符串是否互为 Anagram

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

Java程序检查两个字符串是否互为 Anagram

编写一个函数来检查两个给定的字符串是否是彼此的字谜。一个字符串的变位词是另一个包含相同字符的字符串,只是字符的顺序可以不同。例如,“abcd”和“dabc”是彼此的字谜。

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

我们强烈建议您单击此处并进行练习,然后再继续使用解决方案。

方法一(使用排序):

  1. 对两个字符串进行排序
  2. 比较排序后的字符串

下面是上述思想的实现:

Java
// Java program to check whether two strings
// are anagrams of each other
import java.io.*;
import java.util.Arrays;
import java.util.Collections;
class GFG
{
    /* Function to check whether two strings
       are anagram of each other */
    static boolean areAnagram(char[] str1,
                              char[] str2)
    {
        // Get lengths of both strings
        int n1 = str1.length;
        int n2 = str2.length;
 
        // If length of both strings is not
        // same, then they cannot be anagram
        if (n1 != n2)
            return false;
 
        // Sort both strings
        Arrays.sort(str1);
        Arrays.sort(str2);
 
        // Compare sorted strings
        for (int i = 0; i < n1; i++)
            if (str1[i] != str2[i])
                return false;
 
        return true;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        char str1[] = {'t', 'e', 's', 't'};
        char str2[] = {'t', 't', 'e', 'w'};
       
        // Function Call
        if (areAnagram(str1, str2))
            System.out.println(
            "The two strings are" +
            " anagram of each other");
        else
            System.out.println(
            "The two strings are not" +
            " anagram of each other");
    }
}
// This code is contributed by Nikita Tiwari.


Java
// Java program to check if two strings
// are anagrams of each other
import java.io.*;
import java.util.*;
class GFG
{
    static int NO_OF_CHARS = 256;
 
    /* Function to check whether two strings
       are anagram of each other */
    static boolean areAnagram(char str1[],
                              char str2[])
    {
        // Create 2 count arrays and initialize
        // all values as 0
        int count1[] = new int[NO_OF_CHARS];
        Arrays.fill(count1, 0);
        int count2[] = new int[NO_OF_CHARS];
        Arrays.fill(count2, 0);
        int i;
 
        // For each character in input strings,
        // increment count in the corresponding
        // count array
        for (i = 0; i < str1.length &&
             i < str2.length; i++)
        {
            count1[str1[i]]++;
            count2[str2[i]]++;
        }
 
        // If both strings are of different length.
        // Removing this condition will make the
        // program fail for strings like "aaca"
        // and "aca"
        if (str1.length != str2.length)
            return false;
 
        // Compare count arrays
        for (i = 0; i < NO_OF_CHARS; i++)
            if (count1[i] != count2[i])
                return false;
 
        return true;
    }
 
    // Driver code
    public static void main(String args[])
    {
        char str1[] =
        ("geeksforgeeks").toCharArray();
        char str2[] =
        ("forgeeksgeeks").toCharArray();
 
        // Function call
        if (areAnagram(str1, str2))
            System.out.println(
            "The two strings are" +
            "anagram of each other");
        else
            System.out.println(
            "The two strings are not" +
            " anagram of each other");
    }
}
// This code is contributed by Nikita Tiwari.


Java
// Java program to check if two strings
// are anagrams of each other
class GFG{
 
static int NO_OF_CHARS = 256;
 
// Function to check if two strings
// are anagrams of each other
static boolean areAnagram(char[] str1,
                          char[] str2)
{  
    // Create a count array and initialize
    // all values as 0
    int[] count = new int[NO_OF_CHARS];
    int i;
 
    // For each character in input strings,
    // increment count in the corresponding
    // count array
    for(i = 0; i < str1.length; i++)
    {
        count[str1[i] - 'a']++;
        count[str2[i] - 'a']--;
    }
 
    // If both strings are of different
    // length. Removing this condition
    // will make the program fail for
    // strings like "aaca" and "aca"
    if (str1.length != str2.length)
        return false;
 
    // See if there is any non-zero
    // value in count array
    for(i = 0; i < NO_OF_CHARS; i++)
        if (count[i] != 0)
        {
            return false;
        }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    char str1[] =
    "geeksforgeeks".toCharArray();
    char str2[] =
    "forgeeksgeeks".toCharArray();
 
    // Function call
    if (areAnagram(str1, str2))
        System.out.print(
        "The two strings are " +
        "anagram of each other");
    else
        System.out.print(
        "The two strings are " +
        "not anagram of each other");
}
}
// This code is contributed by mark_85


输出:

The two strings are not anagram of each other

时间复杂度: O(nLogn)

方法2(计数字符):
此方法假定两个字符串中可能的字符集都很小。在下面的实现中,假设字符使用 8 位存储,可能有 256 个字符。

  1. 为两个字符串创建大小为 256 的计数数组。将计数数组中的所有值初始化为 0。
  2. 遍历两个字符串的每个字符并增加相应计数数组中的字符计数。
  3. 比较计数数组。如果两个计数数组相同,则返回 true。

下面是上述思想的实现:

Java

// Java program to check if two strings
// are anagrams of each other
import java.io.*;
import java.util.*;
class GFG
{
    static int NO_OF_CHARS = 256;
 
    /* Function to check whether two strings
       are anagram of each other */
    static boolean areAnagram(char str1[],
                              char str2[])
    {
        // Create 2 count arrays and initialize
        // all values as 0
        int count1[] = new int[NO_OF_CHARS];
        Arrays.fill(count1, 0);
        int count2[] = new int[NO_OF_CHARS];
        Arrays.fill(count2, 0);
        int i;
 
        // For each character in input strings,
        // increment count in the corresponding
        // count array
        for (i = 0; i < str1.length &&
             i < str2.length; i++)
        {
            count1[str1[i]]++;
            count2[str2[i]]++;
        }
 
        // If both strings are of different length.
        // Removing this condition will make the
        // program fail for strings like "aaca"
        // and "aca"
        if (str1.length != str2.length)
            return false;
 
        // Compare count arrays
        for (i = 0; i < NO_OF_CHARS; i++)
            if (count1[i] != count2[i])
                return false;
 
        return true;
    }
 
    // Driver code
    public static void main(String args[])
    {
        char str1[] =
        ("geeksforgeeks").toCharArray();
        char str2[] =
        ("forgeeksgeeks").toCharArray();
 
        // Function call
        if (areAnagram(str1, str2))
            System.out.println(
            "The two strings are" +
            "anagram of each other");
        else
            System.out.println(
            "The two strings are not" +
            " anagram of each other");
    }
}
// This code is contributed by Nikita Tiwari.

输出:

The two strings are anagram of each other

方法3(使用一个数组计算字符):
上述实现可以进一步只使用一个计数数组而不是两个。我们可以为 str1 中的字符递增 count 数组中的值,为 str2 中的字符递减。最后,如果所有计数值都是 0,那么这两个字符串是彼此的字谜。感谢Ace提出此优化建议。

Java

// Java program to check if two strings
// are anagrams of each other
class GFG{
 
static int NO_OF_CHARS = 256;
 
// Function to check if two strings
// are anagrams of each other
static boolean areAnagram(char[] str1,
                          char[] str2)
{  
    // Create a count array and initialize
    // all values as 0
    int[] count = new int[NO_OF_CHARS];
    int i;
 
    // For each character in input strings,
    // increment count in the corresponding
    // count array
    for(i = 0; i < str1.length; i++)
    {
        count[str1[i] - 'a']++;
        count[str2[i] - 'a']--;
    }
 
    // If both strings are of different
    // length. Removing this condition
    // will make the program fail for
    // strings like "aaca" and "aca"
    if (str1.length != str2.length)
        return false;
 
    // See if there is any non-zero
    // value in count array
    for(i = 0; i < NO_OF_CHARS; i++)
        if (count[i] != 0)
        {
            return false;
        }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    char str1[] =
    "geeksforgeeks".toCharArray();
    char str2[] =
    "forgeeksgeeks".toCharArray();
 
    // Function call
    if (areAnagram(str1, str2))
        System.out.print(
        "The two strings are " +
        "anagram of each other");
    else
        System.out.print(
        "The two strings are " +
        "not anagram of each other");
}
}
// This code is contributed by mark_85

输出:

The two strings are anagram of each other

时间复杂度: O(n)
有关详细信息,请参阅关于检查两个字符串是否是彼此的字谜的完整文章!