检查一个单词组成的字母是否出现在另一个字符串中 [英] Check if letters that a word consists of are present in another string

查看:159
本文介绍了检查一个单词组成的字母是否出现在另一个字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次在这里发帖,所以请告诉我是否需要纠正这篇文章中的任何内容。我想请问一些给我一些麻烦的事情。我需要检查一个字符串的字符是否出现在另一个字符串中,但是只需要检测一次。例如,对于字符串BFEABLDEG,LABEL不应该像当前正在做的那样返回true。

为了澄清,目标不是让程序计数字符。程序检查创建一个单词所需的字母是否包含在randomString中,两个字符串中的每个字母的确切编号相同。该计划部分基于游戏节目Countdown。



这是我迄今为止。任何帮助将不胜感激。编辑:感谢所有帮助过我的人。我接受阿鲁的贡献作为我寻找的解决方案,因为它避免了我最准确的问题,因为需要检查的字符串的大小。

  public static boolean Checkword(){
String randomString =BFEABLDEG;
String word =LABEL;
{
for(int i = 0; i< word.length(); i ++){
if(randomString.contains(word.substring((i)))){
返回true;

}
}
return false;

code










$ b $ p


$ b

好的,我给出的解决方案的基本例子。然而,最终目标是让用户从九个随机字符串中创建任意长度的单词。目前他们可以通过输入比字符串中更多的字符。我想知道是否有人可以帮助我,因为已经添加了新的代码。

  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){
返回false;

}
}
返回true;



$ div $解析方案

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



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

即使您添加不在 randomString word $ c>,函数将返回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){
返回false;
}
}
返回true;





$ b

检查重复字符的简单方法是删除一个字符串中的字符。确实有更高效的解决方案,确保检查评论中链接的线程。

  public static void main(String [] args)throws Exception {
System.out.println( BFEABLDEG,LABEL));

$ b $ 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),);
}
返回true;
}


This is my first time posting here, so please tell me if I need to correct anything in this post. I'd like to ask for help with something that's been giving me some trouble. I need to check that the characters of one string appear in another string, but they need to be detected only once. For example, for the string "BFEABLDEG", "LABEL" should not return true, as it currently is doing.

For clarification, the aim is not to have the program count characters. It is to have the program check that the letters needed to create a word are contained in randomString, with the exact same number of each letter in both Strings. The program is partly based on the game show Countdown.

This is what I have so far. Any help would be greatly appreciated.

Edit: Thanks to everyone who helped me out with this. I've accepted Aru's contribution as the solution I was looking for as it avoids the problem I was having most accurately, given that the size of the string that needs to be checked.

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;
 }

}

Ok, the solution I was given works for basic examples. However, the end goal was for the user to make words of any length from a string of nine random characters. Currently they can do this by putting in more occurences of any character than there are in the string. I was wondering if anyone could help me with this, given the new code that the function has been added to.

    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;
     }
}

解决方案

I'm not sure if I entirely understand what you're trying to achive, but the whole logic of your method is flawed.

One problem is, obviously, that your function will return true if just the last character matches, since substring(word.length() - 1) will check whether the last character is contained in the other string. In every other loop, you are checking whether an entire sequence is contained, starting with the entire string and reducing the amount of characters every loop.

Even if you add characters to word that are not in randomString, the function will return true as long as they are not at the end of the string.

Something like this should be what you were looking for originally:

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;
}

A simple solution to also check for duplicated characters is to remove one occurrence of the character in the string. There are certainly more efficient solutions possible, make sure to check the thread linked in the comments.

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;
}

这篇关于检查一个单词组成的字母是否出现在另一个字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆