0

I am trying to write a method that takes an input of a string, and returns a list of possible strings in which logical spaces have been included by checking to see if parts of the string match dictionary words. For example:

Example:

input: "becausetodayuseat" 
Output: {
    "be cause to day us eat ",
    "be cause to day use at ",
    "be cause today us eat ",
    "be cause today use at ",
    "because to day us eat ",
    "because to day use at ",
    "because today us eat ",
    "because today use at "
}

My code is currently

public static String[] space(String[] dict, String s) {
    LinkedList<String> ret = new LinkedList<String>();

    // base case

    if (s.length() == 0) {
        String[] r = { "" };
        return r;
    }


    for (int i = 1; i < s.length(); i++) {
        String prefix = s.substring(0, i);
        if (inDictionary(dict, prefix)) {
            prefix = prefix + " ";
            ret.add(prefix);
            String suffix = s.substring(i);
            String[] end = space(dict,suffix);
            //System.out.println(end.length);
            for (int j = 0; j < end.length; ++j) {
                ret.add(end[j]);
            }

        }
    }

    // This line converts LinkedList<String> to String []
    return ret.toArray(new String[0]);

I know the for loop is the problem but I can't seem to find the bug. I am printing out

be 
cause 
to 
day 
us 
use 
a 
today 
us 
use 
a 
because 
to 
day 
us 
use 
a 
today 
us 
use 
a 

Any help would be greatly appreciated :)

Prune
  • 76,765
  • 14
  • 60
  • 81
cart2003
  • 9
  • 1
  • Care to elaborate what exactly is not working for you. i don't see a question in your post – Coder Mar 15 '17 at 19:24
  • I reformatted the expected and actual output; I hope that clarifies things. – Prune Mar 15 '17 at 19:30
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. Specifically, your code as posted is supposed to produce the given output. Yours doesn't run on its own. – Prune Mar 15 '17 at 19:32
  • 1
    I suggest that you write a driver program that builds a tiny dictionary and calls your routine. Also insert some useful tracing print statements to show what's happening in the program flow. – Prune Mar 15 '17 at 19:33

3 Answers3

0

Thinking it through without providing the actual implementation:

At each step we can either add a space, or not add a space. We can think of the problem as a binary tree. At each step in the tree we add a space if the current "active sequence" is a word, and we also try not adding a space. Let's look at the input because

b is not a word so we proceed to "" + space("because") which is a word so now we have "be " + space("cause") and space("because").

And we continue until the base case.

marisbest2
  • 1,346
  • 2
  • 17
  • 30
0

Edit : sorry, read to fast, it gives only one result. I think again about it

Try this

public String parse(String s, HashMap<String, String> map,String dict[]) {
    if (map.containsKey(s)) {
        return map.get(s);
    }
    if (inDictionary(s)) {
        return s;
    }
    for (int left = 1; left < s.length(); left++) {
        String leftSub = s.substring(0, left);
        if (!inDictionary(leftSub)) {
            continue;
        }
        String rightSub = s.substring(left);
        String rightParsed = parse(rightSub, map,dict);
        if (rightParsed != null) {
            String parsed = leftSub + " " + rightParsed;
            map.put(s, parsed);
            return parsed;
        }
    }
    map.put(s, null);
    return null;
 }

To call it: Your_object.parse(Your_string,new HashMap(),Your_dict)

Incognito
  • 133
  • 1
  • 10
0

You are now adding prefix and suffixes separately. Once you find a word, you need to append all possible suffixes to it and then add it to your collection.

Pseudocode:

if word found
    foreach end in ends
        result.add(word + ' ' + end)