-7

The problem is as follows:

You are given a dictionary which contains a list of words and has the method .contains() which returns a boolean indicating if the word is in the dictionary or not. The actual implementation of the dictionary doesn´t matter for the problem.

The input is a string of words which all spaces are removed and contains words in a dictionary. However, it may also contain characters which aren´t found in the dictionary. The output must be a String with the words separated by a space and any word that is not found in the dictionary must be joined with a word which is found in the dictionary.

For example:

Diccionary = ["hi", "mike", "java"]

Input = "HiMikeJava"
Output = "Hi Mike Java"

Input = "HiMikeLJava"
Output = "Hi MikeL Java"

Input = "HiMikeLJavaSS"
Output = "Hi MikeL JavaSS"

The problem that I find is that the input could contain characters not found in the dictionary. Any help is appreciated.

Note: If you answer in code, please answer in Java since it is the only programming language I know. Thanks.

Community
  • 1
  • 1
user132226
  • 11
  • 1
  • 1

2 Answers2

1

I've just done this, so I have a solution... it's not very elegant but it works. That said, this is your homework so you have to do the work!

But, some pointers:

  1. Create a method which takes an array of String inputs
  2. Create an outer loop to run while the input has chars in it
  3. Create an inner loop to go through each word
  4. In this loop take a substring of input the length of the word you are looking at and compare it to the word (Think: How will you handle the fact that the cases might be different?)
  5. If they match - great print a space and then the substring and remove the substring from input.
  6. If none of them match then print the first character of the input and remove this character from the input (How will you know whether it has been found or not? Do you need an extra variable)

If you implement it exactly like this there might be some cases where you will get an IndexOutOfBoundsException. Also, you will have an unwanted space before the first word. I leave this to you to figure out, granted it's homework!

halfer
  • 19,824
  • 17
  • 99
  • 186
saml
  • 794
  • 1
  • 9
  • 20
0
    /// <summary>
    /// Word Break Problem
    /// </summary>
    /// <param name="inputDictionary"></param>
    /// <param name="inputString"></param>
    /// <param name="outputString"></param>
    public static void WordBreakProblem(List<string> inputDictionary, string inputString, string outputString)
    {
        if (inputString.Length == 0)
        {
            Console.WriteLine(outputString);
            return;
        }

        for (int i = 1; i <= inputString.Length; i++)
        {
            string subString = inputString.Substring(0, i);

            if (inputDictionary.Contains(subString, StringComparer.OrdinalIgnoreCase))
                WordBreakProblem(inputDictionary, inputString[i..], outputString + " " + subString);
        }
    }