-6

Suppose you have a dictionary that contains valid words.

Given an input string with all spaces removed, determine whether the string is composed of valid words or not.

You can assume the dictionary is a hashtable that provides O(1) lookup.

Please give a recurrence relation for this. I found this question in a book , but the book gives no answer?

Shai
  • 111,146
  • 38
  • 238
  • 371
Programmer
  • 6,565
  • 25
  • 78
  • 125

2 Answers2

2
IsWordValid(S) = for word in dict:
                    if S.startsWith(word) and IsWordValid(S[word.length:])
                          return true
                 return false
IsWordValid(null) = true
dfb
  • 13,133
  • 2
  • 31
  • 52
  • Is this a Recurrence relation. – Programmer Apr 06 '11 at 19:02
  • a dynamic programming recurrence relation is generally top down. This one is bottom up. I dont know how it appears so – Programmer Apr 06 '11 at 19:14
  • @spinning_plate I don't think this is correct. You want to return _true_ if **any** word in the dictionary that is a prefix leads to a valid segmentation. You're returning as soon as you find a prefix, regardless of if it is an answer or not. – abeln Apr 06 '11 at 19:57
  • @Programmer - how is it bottom up? It starts from the full string and decomposes it in to subproblems? – dfb Apr 06 '11 at 20:35
0

Here is a code in Mathematica I started to develop for a recent code golf.
It is a minimal matching, non greedy, recursive algorithm. That means that the sentence "the pen is mighter than the sword" (without spaces) returns {"the pen is might er than the sword} :)

findAll[s_] :=
  Module[{a = s, b = "", c, sy = "="},
  While[
   StringLength[a] != 0,
   j = "";
   While[(c = findFirst[a]) == {} && StringLength[a] != 0,
    j = j <> StringTake[a, 1];
    sy = "~";
    a = StringDrop[a, 1];
   ];
   b = b <> " " <> j ;
   If[c != {},
    b = b <> " " <> c[[1]];
    a = StringDrop[a, StringLength[c[[1]]]];
   ];
  ];
   Return[{StringTrim[StringReplace[b, "  " -> " "]], sy}];
]

findFirst[s_] :=
  If[s != "" && (c = DictionaryLookup[s]) == {}, 
   findFirst[StringDrop[s, -1]], Return[c]];

Sample Input

ss = {"twodreamstop", 
      "onebackstop", 
      "butterfingers", 
      "dependentrelationship", 
      "payperiodmatchcode", 
      "labordistributioncodedesc", 
      "benefitcalcrulecodedesc", 
      "psaddresstype", 
      "ageconrolnoticeperiod",
      "month05", 
      "as_benefits", 
      "fname"}

Output

 twodreamstop              = two dreams top
 onebackstop               = one backstop
 butterfingers             = butterfingers
 dependentrelationship     = dependent relationship
 payperiodmatchcode        = pay period match code
 labordistributioncodedesc ~ labor distribution coded es c
 benefitcalcrulecodedesc   ~ benefit c a lc rule coded es c
 psaddresstype             ~ p sad dress type
 ageconrolnoticeperiod     ~ age con rol notice period
 month05                   ~ month 05
 as_benefits               ~ as _ benefits
 fname                     ~ f name

HTH

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190