检查一个单词所包含的字母是否出现在另一个字符串中



这是我第一次在这里发帖,所以如果我在这篇文章中有什么需要纠正的地方,请告诉我。我想请您帮我解决一件一直困扰我的事。我需要检查一个字符串的字符是否出现在另一个字符串中,但它们只需要检测一次。例如,对于字符串"BFEABLDEG", "LABEL"不应该像现在这样返回true。

澄清一下,目的不是让程序计数字符。它是让程序检查创建单词所需的字母是否包含在randomString中,并且两个string中的每个字母的数目完全相同。该节目部分取材于游戏节目《倒计时》。

这是我目前所知道的。任何帮助都将非常感激。

编辑:感谢所有帮助我解决这个问题的人。我已经接受了Aru的贡献作为我正在寻找的解决方案,因为它避免了我最准确地遇到的问题,考虑到需要检查字符串的大小。
public static boolean Checkword(){
String randomString = "BFEABLDEG";
 String word = "LABEL";
 {
 for(int i=0;i<word.length(); i++){
      if(randomString.contains(word.substring((i)))){
          return true;
      }
 }
return false;
 }

}

好的,我给出的解决方案适用于基本的例子。然而,最终目标是让用户从9个随机字符的字符串中生成任意长度的单词。目前,他们可以通过添加比字符串中出现次数更多的字符来实现这一点。我想知道是否有人可以帮助我这个,给定的新代码,该功能已被添加到。

    public static boolean Checkword(String x){
    String randomString = convertToString();
     String word = x;
     {
     for(int i=0;i<word.length(); i++){
          if(randomString.indexOf(word.charAt(i)) == -1){
              return false;
          }
     }
    return true;
     }
}

我不确定我是否完全理解你想要达到的目的,但是你的方法的整个逻辑是有缺陷的。

显然,一个问题是,如果只有最后一个字符匹配,您的函数将返回true,因为substring(word.length() - 1)将检查最后一个字符是否包含在另一个字符串中。在其他每个循环中,检查是否包含整个序列,从整个字符串开始,并减少每个循环的字符数量。

即使您在word中添加了不在randomString中的字符,只要它们不在字符串的末尾,该函数将返回true。

像这样的东西应该是你最初想要的:

public static boolean checkWord() {
    String randomString = "BFEABLDEG";
    String word = "LABEL";
    for (int i = 0; i < word.length(); i++) {
        if (randomString.indexOf(word.charAt(i)) == -1) {
            return false;
        }
    }
    return true;
}

检查重复字符的一个简单解决方案是删除字符串中该字符的一次出现。当然有更有效的解决方案,请务必查看评论中的链接。

public static void main(String[] args) throws Exception {
    System.out.println(test("BFEABLDEG", "LABEL"));
}
public static boolean test(String searchIn, String searchFor) {
    for (char c : searchFor.toCharArray()) {
        if (searchIn.indexOf(c) == -1) {
            return false;
        }
        searchIn = searchIn.replaceFirst(Character.toString(c), "");
    }
    return true;
}

它返回true,因为你测试的是一个字符的randomString

public static boolean Checkword( String pattern, String randomString ){
  return ( randomString .contains( pattern ) ) ? true : false;
}
String pattern = "LABEL";
String randomString = "BFEABLDEG";
Checkword( pattern, randomString );
//return false
randomString = "AZDAZLABELIIDZA";
Checkword( pattern, randomString );
//return true

如果word变量中的任何字符包含在randomString变量中,则程序返回true。从你的评论来看,听起来你想检查你的word字符串中的每个字符是否包含在你的randomString变量中。这里有一个稍微不同的方法。

public static boolean Checkword(){
    String randomString = "BFEABLDEG";
    String word = "LABEL";
    for(int i=0;i<word.length(); i++){
         if(randomString.indexOf(word.charAt(i)) != -1){
             //If the letter isn't contained return false
             return false;
          } else {
              //If the letter is contained remove it from the string
              int charLocation = randomString.indexOf(word.charAt(i));
              randomString = randomString.substring(0, charLocation) + randomString.substring(charLocation+1);
          }
    }
    //If I haven't returned yet, then every letter is contained, return true
    return true;
}

这是相当低效的,因为它每次都必须创建一个新的字符串,如果你想让它更好,使用字符串生成器来改变字符串,以删除发现的字符。

最新更新