user3424512 user3424512 - 1 month ago 4
Java Question

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

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

Answer

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