猿问

如何检查两个单词是否是字谜

我有一个程序,可以显示两个单词是否是彼此的字谜。有一些例子不能正常工作,我会感激任何帮助,虽然如果它不是先进的那将是伟大的,因为我是一年级程序员。“校长”和“教室”是彼此的字谜,然而当我把“教室”改为“theclafsroom”时,它仍然说它们是字谜,我做错了什么?


import java.util.ArrayList;

public class AnagramCheck

{

  public static void main(String args[])

  {

      String phrase1 = "tbeclassroom";

      phrase1 = (phrase1.toLowerCase()).trim();

      char[] phrase1Arr = phrase1.toCharArray();


      String phrase2 = "schoolmaster";

      phrase2 = (phrase2.toLowerCase()).trim();

      ArrayList<Character> phrase2ArrList = convertStringToArraylist(phrase2);


      if (phrase1.length() != phrase2.length()) 

      {

          System.out.print("There is no anagram present.");

      } 

      else 

      {

          boolean isFound = true;

          for (int i=0; i<phrase1Arr.length; i++)

          {  

              for(int j = 0; j < phrase2ArrList.size(); j++) 

              {

                  if(phrase1Arr[i] == phrase2ArrList.get(j))

                  {

                      System.out.print("There is a common element.\n");

                      isFound = ;

                      phrase2ArrList.remove(j);

                  }

              }

              if(isFound == false)

              {

                  System.out.print("There are no anagrams present.");

                  return;

              } 

          }

          System.out.printf("%s is an anagram of %s", phrase1, phrase2);

      }

  }


  public static ArrayList<Character> convertStringToArraylist(String str) {

      ArrayList<Character> charList = new ArrayList<Character>(); 

      for(int i = 0; i<str.length();i++){

          charList.add(str.charAt(i));

      }

      return charList;

  }

}


SMILET
浏览 842回答 3
3回答

慕田峪9158850

最快的算法是将26个英文字符中的每一个映射到唯一的素数。然后计算字符串的乘积。根据算术的基本定理,当且仅当它们的产品相同时,2个字符串是字谜。

largeQ

如果两个单词包含相同数量的字符和相同的字符,则它们是彼此的字谜。您只需要按字典顺序对字符进行排序,并确定一个字符串中的所有字符是否与另一个字符串中的所有字符相同且顺序相同。这是一个代码示例。Arrays在API中查看以了解这里发生了什么。public boolean isAnagram(String firstWord, String secondWord) {&nbsp; &nbsp; &nbsp;char[] word1 = firstWord.replaceAll("[\\s]", "").toCharArray();&nbsp; &nbsp; &nbsp;char[] word2 = secondWord.replaceAll("[\\s]", "").toCharArray();&nbsp; &nbsp; &nbsp;Arrays.sort(word1);&nbsp; &nbsp; &nbsp;Arrays.sort(word2);&nbsp; &nbsp; &nbsp;return Arrays.equals(word1, word2);}

GCT1015

如果对任一阵列进行排序,则解决方案将变为O(n log n)。但是如果你使用一个hashmap,那就是O(n)。经过测试和工作。char[] word1 = "test".toCharArray();char[] word2 = "tes".toCharArray();Map<Character, Integer> lettersInWord1 = new HashMap<Character, Integer>();for (char c : word1) {&nbsp; &nbsp; int count = 1;&nbsp; &nbsp; if (lettersInWord1.containsKey(c)) {&nbsp; &nbsp; &nbsp; &nbsp; count = lettersInWord1.get(c) + 1;&nbsp; &nbsp; }&nbsp; &nbsp; lettersInWord1.put(c, count);}for (char c : word2) {&nbsp; &nbsp; int count = -1;&nbsp; &nbsp; if (lettersInWord1.containsKey(c)) {&nbsp; &nbsp; &nbsp; &nbsp; count = lettersInWord1.get(c) - 1;&nbsp; &nbsp; }&nbsp; &nbsp; lettersInWord1.put(c, count);}for (char c : lettersInWord1.keySet()) {&nbsp; &nbsp; if (lettersInWord1.get(c) != 0) {&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}return true;
随时随地看视频慕课网APP
我要回答