827. Expressive Words
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to identify how many words from a given list are "stretchy" with respect to a string s. A word is considered stretchy if it can be transformed into s by extending groups of identical, consecutive characters. The key rule for extension is that a character group can be lengthened only if the resulting group in s has a length of three or more.
Solution
Expressive Words in Python
class Solution:
def expressiveWords(self, s: str, words: List[str]) -> int:
def is_stretchy(word: str) -> bool:
"""Helper function to check if a word is stretchy with respect to s."""
i, j = 0, 0 # Pointers for s and word
n, m = len(s), len(word)
while i < n and j < m:
# If characters don't match, it's impossible for the word to be stretchy.
if s[i] != word[j]:
return False
# Find the length of the group of consecutive characters in s.
current_char = s[i]
len_s = 0
while i < n and s[i] == current_char:
len_s += 1
i += 1
# Find the length of the group of consecutive characters in word.
len_word = 0
while j < m and word[j] == current_char:
len_word += 1
j += 1
# Check the stretchy conditions.
# 1. The group in s must be at least as long as in word.
if len_s < len_word:
return False
# 2. If the group in s is longer, it must be of length 3 or more.
if len_s > len_word and len_s < 3:
return False
# After iterating, both pointers must be at the end of their respective strings.
# This ensures that the sequence of character groups is identical.
return i == n and j == m
stretchy_count = 0
for word in words:
if is_stretchy(word):
stretchy_count += 1
return stretchy_count
Expressive Words in Java
class Solution {
public int expressiveWords(String s, String[] words) {
int stretchyCount = 0;
for (String word : words) {
if (isStretchy(s, word)) {
stretchyCount++;
}
}
return stretchyCount;
}
// Helper function to check if a word is stretchy with respect to s.
private boolean isStretchy(String s, String word) {
int i = 0, j = 0; // Pointers for s and word
int n = s.length(), m = word.length();
while (i < n && j < m) {
// If characters don't match, it's impossible for the word to be stretchy.
if (s.charAt(i) != word.charAt(j)) {
return false;
}
// Find the length of the group of consecutive characters in s.
char currentChar = s.charAt(i);
int lenS = 0;
while (i < n && s.charAt(i) == currentChar) {
lenS++;
i++;
}
// Find the length of the group of consecutive characters in word.
int lenWord = 0;
while (j < m && word.charAt(j) == currentChar) {
lenWord++;
j++;
}
// Check the stretchy conditions.
// 1. The group in s must be at least as long as in word.
if (lenS < lenWord) {
return false;
}
// 2. If the group in s is longer, it must be of length 3 or more.
if (lenS > lenWord && lenS < 3) {
return false;
}
}
// After iterating, both pointers must be at the end of their respective strings.
// This ensures that the sequence of character groups is identical.
return i == n && j == m;
}
}
Expressive Words in C++
class Solution {
public:
int expressiveWords(string s, vector<string>& words) {
int stretchy_count = 0;
for (const string& word : words) {
if (check(s, word)) {
stretchy_count++;
}
}
return stretchy_count;
}
private:
// Helper function to check if a word is stretchy with respect to s.
bool check(const string& s, const string& word) {
int i = 0, j = 0; // Pointers for s and word
int n = s.length(), m = word.length();
while (i < n && j < m) {
// If characters don't match, it's impossible for the word to be stretchy.
if (s[i] != word[j]) {
return false;
}
// Find the length of the group of consecutive characters in s.
char current_char = s[i];
int len_s = 0;
while (i < n && s[i] == current_char) {
len_s++;
i++;
}
// Find the length of the group of consecutive characters in word.
int len_word = 0;
while (j < m && word[j] == current_char) {
len_word++;
j++;
}
// Check the stretchy conditions.
// 1. The group in s must be at least as long as in word.
if (len_s < len_word) {
return false;
}
// 2. If the group in s is longer, it must be of length 3 or more.
if (len_s > len_word && len_s < 3) {
return false;
}
}
// After iterating, both pointers must be at the end of their respective strings.
// This ensures that the sequence of character groups is identical.
return i == n && j == m;
}
};
Expressive Words in TypeScript
function expressiveWords(s: string, words: string[]): number {
const isStretchy = (word: string): boolean => {
let i = 0, j = 0; // Pointers for s and word
const n = s.length, m = word.length;
while (i < n && j < m) {
// If characters don't match, it's impossible for the word to be stretchy.
if (s[i] !== word[j]) {
return false;
}
// Find the length of the group of consecutive characters in s.
const currentChar = s[i];
let lenS = 0;
while (i < n && s[i] === currentChar) {
lenS++;
i++;
}
// Find the length of the group of consecutive characters in word.
let lenWord = 0;
while (j < m && word[j] === currentChar) {
lenWord++;
j++;
}
// Check the stretchy conditions.
// 1. The group in s must be at least as long as in word.
if (lenS < lenWord) {
return false;
}
// 2. If the group in s is longer, it must be of length 3 or more.
if (lenS > lenWord && lenS < 3) {
return false;
}
}
// After iterating, both pointers must be at the end of their respective strings.
// This ensures that the sequence of character groups is identical.
return i === n && j === m;
};
let stretchyCount = 0;
for (const word of words) {
if (isStretchy(word)) {
stretchyCount++;
}
}
return stretchyCount;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.