
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check a string can be broken into given list of words or not in python
Suppose we have a word list and another string s with no spaces. We have to check whether the string can be broken down using the list of words or not.
So, if the input is like words = ["love", "python", "we", "programming", "language"] s = "welovepythonprogramming", then the output will be True
To solve this, we will follow these steps −
- words := a new set of all unique words
- Define a function rec() . This will take i
- if i is same as size of s , then
- return True
- acc := blank string
- for j in range i to size of s, do
- acc := acc concatenate s[j]
- if acc is in words, then
- if rec(j + 1) is True, then
- return True
- if rec(j + 1) is True, then
- return False
- From the main method call rec(0) and return result
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, words, s): words = set(words) def rec(i=0): if i == len(s): return True acc = "" for j in range(i, len(s)): acc += s[j] if acc in words: if rec(j + 1): return True return False return rec() ob = Solution() words = ["love", "python", "we", "programming", "language"] s = "welovepythonprogramming" print(ob.solve(words, s))
Input
["love", "python", "we", "programming", "language"], "welovepythonprogramming"
Output
True
- Related Articles
- Program to check a string can be split into three palindromes or not in Python
- Program to check words can be found in matrix character board or not in Python
- Program to check we can spell out the target by a list of words or not in Python
- Program to check given string is pangram or not in Python
- Check if a two-character string can be made using given words in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Python program to check whether a given string is Heterogram or not
- Python program to check if a given string is Keyword or not
- Program to check given string is anagram of palindromic or not in Python
- C++ Program to check string can be reduced to 2022 or not
- Program to check all tasks can be executed using given server cores or not in Python
- Program to check whether given words are maintaining given pattern or not in C++
- Check if given string can be formed by concatenating string elements of list in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check whether final string can be formed using other two strings or not in Python

Advertisements