Similar Problems

Similar Problems not available

Number Of Valid Words In A Sentence - Leetcode Solution

Companies:

LeetCode:  Number Of Valid Words In A Sentence Leetcode Solution

Difficulty: Easy

Topics: string  

Problem Statement:

Given a string sentence containing only lowercase letters and spaces, return the number of possible valid words that can be formed from sentence. A valid word is any substring that is at least 2 characters long and is a palindrome. For example, "abba" is a palindrome, but "xyz" is not. A substring is a contiguous sequence of characters within a string.

Example:

Input: sentence = "abba cca bbcazzbb" Output: 4 Explanation: There are 4 valid words: "abba", "cc", "cazz", and "bb"

Solution:

To solve this problem, we need to split the sentence into words, and then check if each word is a palindrome, or if it can be split into smaller palindromic substrings. We can do this using dynamic programming.

Algorithm:

  1. Split the sentence into words.
  2. For each word, check if it is a palindrome or can be split into smaller palindromic substrings.
  3. Count the number of valid words.

Let's break down the above algorithm into smaller parts.

Part 1: Split the sentence into words

We can use the split() function to split the sentence into words. This function splits a string into a list of substrings based on a delimiter string.

Example:

sentence = "abba cca bbcazzbb" words = sentence.split(' ') print(words)

Output:

['abba', 'cca', 'bbcazzbb']

Part 2: Check if a word is a palindrome or can be split into palindromic substrings

We can use dynamic programming to check if a word is a palindrome or can be split into smaller palindromic substrings.

We can define a boolean array P[i][j], where P[i][j] is true if the substring s[i:j+1] is a palindrome, and false otherwise.

To fill up the boolean array, we can use the following recurrence relation:

P[i][j] = (s[i] == s[j]) and P[i+1][j-1]

To check if a word can be split into smaller palindromic substrings, we can use a similar dynamic programming approach.

We can define a boolean array S[i][j], where S[i][j] is true if the substring s[i:j+1] can be split into smaller palindromic substrings, and false otherwise.

To fill up the boolean array, we can use the following recurrence relation:

S[i][j] = (P[i][j]) or (S[i][k] and S[k+1][j]) for i <= k < j

The above recurrence relation checks if the substring s[i:j+1] can be split into smaller palindromic substrings by checking all possible splits at index k.

Part 3: Count the number of valid words

We can count the number of valid words by iterating over each word, and checking if it is a palindrome or can be split into smaller palindromic substrings.

Code:

Here is the Python code for solving the problem:

def countPalindromicWords(sentence): words = sentence.split(' ') count = 0 # define boolean arrays for checking palindromes and splits n = len(sentence) P = [[False for j in range(n)] for i in range(n)] S = [[False for j in range(n)] for i in range(n)] # fill up the boolean arrays using dynamic programming for i in range(n): P[i][i] = True S[i][i] = True for L in range(2, n+1): for i in range(n-L+1): j = i+L-1 if L == 2: P[i][j] = (sentence[i] == sentence[j]) else: P[i][j] = (sentence[i] == sentence[j]) and P[i+1][j-1] S[i][j] = (P[i][j]) or any(S[i][k] and S[k+1][j] for k in range(i, j)) # count the number of valid words for w in words: if len(w) >= 2 and (P[0][len(w)-1] or S[0][len(w)-1]): count += 1 return count

sentence = "abba cca bbcazzbb" print(countPalindromicWords(sentence)) # output: 4

Time Complexity:

The time complexity of the solution is O(n^3), where n is the length of the sentence. This is because we use dynamic programming to fill up two boolean arrays, each of size n^2, and iterate over each word of the sentence.

Space Complexity:

The space complexity of the solution is O(n^2), where n is the length of the sentence. This is because we use two boolean arrays, each of size n^2, to store the results of dynamic programming.

Number Of Valid Words In A Sentence Solution Code

1