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

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

本文介绍了如何检查是否两个词是字谜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,向您展示了两个词是否是彼此的字谜。有迹象表明,将无法正常工作的几个例子,我会AP preciate任何帮助,但如果它不先进,将是巨大的,因为我是第一年的程序员。 校长和theclassroom是彼此的字谜游戏,但是当我变theclassroom到theclafsroom还在说他们是字谜,我究竟做错了什么?

 进口的java.util.ArrayList;
公共类AnagramCheck
{
  公共静态无效的主要(字符串的args [])
  {
      字符串phrase1 =tbeclassroom;
      phrase1 =(phrase1.toLowerCase())修剪()。
      的char [] phrase1Arr = phrase1.toCharArray();

      字符串phrase2 =校长;
      phrase2 =(phrase2.toLowerCase())修剪()。
      ArrayList的<性格> phrase2ArrList = convertStringToArraylist(phrase2);

      如果(phrase1.length()!= phrase2.length())
      {
          System.out.print(没有字谜present。);
      }
      其他
      {
          布尔isFound = TRUE;
          的for(int i = 0; I< phrase1Arr.length;我++)
          {
              对于(INT J = 0; J< phrase2ArrList.size(); J ++)
              {
                  如果(phrase1Arr [我] == phrase2ArrList.get(J))
                  {
                      System.out.print(有一个共同的元素\ñ。);
                      isFound =;
                      phrase2ArrList.remove(J);
                  }
              }
              如果(isFound ==假)
              {
                  System.out.print(有没有字谜present。);
                  返回;
              }
          }
          System.out.printf(%s是%S的字谜,phrase1,phrase2);
      }
  }

  公共静态的ArrayList<性格> convertStringToArraylist(字符串str){
      ArrayList的<性格> charlist中的=新的ArrayList<性格>();
      的for(int i = 0; I< str.length();我++){
          charList.add(str.charAt(ⅰ));
      }
      返回charlist中;
  }
}
 

解决方案

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

I have a program that shows you whether two words are anagrams of one another. There are a few examples that will not work properly and I would appreciate any help, although if it were not advanced that would be great, as I am a 1st year programmer. "schoolmaster" and "theclassroom" are anagrams of one another, however when I change "theclassroom" to "theclafsroom" it still says they are anagrams, what am I doing wrong?

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;
  }
}
解决方案

Fastest algorithm would be to map each of the 26 English characters to a unique prime number. Then calculate the product of the string. By the fundamental theorem of arithmetic, 2 strings are anagrams if and only if their products are the same.

这篇关于如何检查是否两个词是字谜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:49