Check if letters that a word consists of are present in another string

后端 未结 3 774
后悔当初
后悔当初 2021-01-29 07:35

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.

相关标签:
3条回答
  • 2021-01-29 08:02

    It is returning true because your testing one char of 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
    
    0 讨论(0)
  • 2021-01-29 08:06

    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;
    }
    
    0 讨论(0)
  • 2021-01-29 08:07

    Your program as it is is returning true if any character in your word variable is contained in your randomString variable. Judging from your comments it sounds like you want to check if every character from your word string is contained within your randomString variable. Here is a slightly different approach.

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

    This is quite inefficient since it has to create a new string each time, if you want to make it a little better, use a string builder to mutate the string to remove the characters as they are found.

    0 讨论(0)
提交回复
热议问题