Leetcode 809: Expressive Words

grid47
grid47
Exploring patterns and algorithms
Aug 18, 2024 7 min read

A string of words where expressive words glow softly, highlighting their expressive nature.
Solution to LeetCode 809: Expressive Words Problem

You are given a string s and a list of query words. Each word in the query list is said to be ‘stretchy’ if it can be transformed into string s by extending groups of adjacent letters. You can extend a group of letters (e.g., ‘a’, ‘bb’, ‘ccc’) as long as its length is at least 3. For example, if s = ‘heeellooo’, you could extend ’e’ and ‘o’ to make ‘hello’ stretchy. Return the number of query strings that can be stretched to become s.
Problem
Approach
Steps
Complexity
Input: You are provided with two inputs: a string s and an array of words. Each word in the array will be checked to see if it can be transformed into s by the described operations.
Example: Input: s = 'aaaabbbb', words = ['ab', 'aaaabbbb', 'aaabbbbb']
Constraints:
• 1 <= s.length, words.length <= 100
• 1 <= words[i].length <= 100
• s and words[i] consist of lowercase letters.
Output: The output should be an integer representing how many words in the query list can be transformed into string s through the described operations.
Example: Output: 2
Constraints:
• The result is an integer count.
Goal: The task is to determine if each word in the query list can be transformed into string s by extending adjacent letter groups according to the given rules.
Steps:
• For each word in the query list, iterate through its characters and compare them to the corresponding characters in s.
• Check if groups of adjacent characters in both the word and s can be extended by applying the transformation rules.
• Count how many words can be transformed into s.
Goal: Ensure the algorithm handles all edge cases and input constraints correctly.
Steps:
• Handle cases where the word is too short to match s.
• Handle cases where the word contains groups that cannot be extended to match s.
Assumptions:
• Words in the query list are non-empty and consist only of lowercase letters.
• The transformation is only possible if groups of characters can be extended by at least 3 characters.
Input: Input: s = 'heeellooo', words = ['hello', 'hi', 'helo']
Explanation: The word 'hello' can be stretched by extending 'e' to 'ee' and 'o' to 'ooo' to match s. The word 'hi' cannot be stretched to match s because it doesn't contain any repeated groups. The word 'helo' cannot match s because the group 'll' is not large enough to be extended.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus