Feature #3: Add White Spaces to Create Words

Implementing the "Add White Spaces to Create Words" feature for our "Search Engine" project.

Description

While searching on the web, we know that users make many spelling errors. One error is combining multiple words. To tackle this issue, your team has decided to create a module that adds white spaces in the query to see if valid words can be created by breaking the original query. This module will be triggered if the original query gives very few results or no results at all. You are not concerned with implementing the triggering function; you only have to implement the break_query functionality, which receives the current query and a list of all possible valid words.

Let’s consider a case where you are given the query "vegancookbook". This query didn’t get any web hits, so now it is sent to the break_query function to check if it’s possible to add white spaces to this query to create valid words. You are also given a dictionary containing all possible words. For this problem, we will assume that the only possible words are ["i", "cream", "cook", "scream", "ice", "cat", "book", "icecream", "vegan"]. Your function should use this list of strings to determine if the original query can be broken down into words provided in the dictionary. Then, the breakQuery function should return a Boolean value depending on whether it is possible to break down the query or not. In the example we just discussed, the function will return true because we can add spaces in the "vegancookbook" to produce "vegan cook book", which contains words from the dictionary.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.