Valid Dictionary Words Algorithm

By | May 24, 2014

The problem is like this :

Given a string str and a dictionary of words determine if str can be segmented into a space-separated
sequence of one or more dictionary words.
Example Input: str = “InterviewGeek”, dict = [“Interview”, “Geek”,”hell”] , Output : true

Algorithm :

  • For each word in the string check if it’s the prefix of String str
  • if it is cut that word from str and continue with the remaining part until length of str becomes zero in that case return true
  • other wise coninue with next word
  • return false

Here’s the code :

or more tutorials see our tutorials page

Leave a Reply

Your email address will not be published. Required fields are marked *