If the operator that checks to see if the user entered the string contains the letters entered, excluding any additional letters

I need to create a program where the user enters a string and 5 letters, and the program checks if the string can be created again from those 5 letters, but ignores any letters from which the string cannot be created.

For example, if I enter the word "hello" and then I enter 5 letters "h", "e", "l", "o" and "p", I want the program to make sure that these letters make up the word "hello" but ignore any additional letters, which in this case is "p". Another example, if I enter the word "apple" and then enter 5 letters "a", "p", "l", "o" and "f", I want the program to recognize that these letters do not create the word apple and ignore the letters "o" and "f".

If the program checks that the string can be created from these 5 letters, ignoring any additional functions, then it will print "You win!", But if this does not lead to the exit "You lose!" Here is the code I have so far:

public static void main(String[] args) {

    Scanner keyboard = new Scanner (System.in);
    System.out.print("Please enter a word: ");
    String in = keyboard.nextLine();
    System.out.print("Please enter the first letter: ");
    String letter1 = keyboard.next();
    System.out.print("Please enter the second letter: ");
    String letter2 = keyboard.next();
    System.out.print("Please enter the third letter: ");
    String letter3 = keyboard.next();
    System.out.print("Please enter the fourth letter: ");
    String letter4 = keyboard.next();
    System.out.print("Please enter the fifth letter: ");
    String letter5 = keyboard.next();

    if (in.contains(letter1 + letter2 + letter3 + letter4 + letter5)) {
        System.out.println("You win!");
    } else {
        System.out.println("You lose!");
    }

}

      

Here is the output I get when I run this:

sample output

I'm new to Java, so I don't know if there is some code I could use to do this. If anyone knows, please take me in the right direction.

+3


source to share


2 answers


The simplest algorithm I can think of is this: for each character entered, remove all occurrences from the string. Finally, check that the line is empty. Or in code:

char[] cs = new char[] {'h', 'e', 'l', 'o', 'p'};
String s = "hello";
for (char c : cs) s = s.replace(c+"", "");
if (s.isEmpty()) System.out.println("You win!");

      



Please note that this approach does not work very well. For a given number of characters n

and string length, m

this is done as O(n*m)

. It also does a lot of costly reallocations String

.

+3


source


The operator letter1 + letter2 + letter3 + letter4 + letter5

concatenates strings. If you stored the results of this in a variable, then this variable will be equal to a string helop

.



I would store the letters 1-5 in some Collection

and then check each character to see if it is in that collection using charAt

. There are other more efficient ways to solve your problem, but I think this is the most clear.

0


source







All Articles