Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Sometimes people repeat letters to represent extra feeling. For example:
"hello" -> "heeellooo""hi" -> "hiiii"In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".
You are given a string s and an array of query strings words. A query word is stretchy if it can be made to be equal to s by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is three or more.
"hello", we could do an extension on the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has a size less than three. Also, we could do another extension like "ll" -> "lllll" to get "helllllooo". If s = "helllllooo", then the query word "hello" would be stretchy because of these two extension operations: query = "hello" -> "hellooo" -> "helllllooo" = s.Return the number of query strings that are stretchy.
Example 1:
Input: s = "heeellooo", words = ["hello", "hi", "helo"] Output: 1 Explanation: We can extend "e" and "o" in the word "hello" to get "heeellooo". We can't extend "helo" to get "heeellooo" because the group "ll" is not size 3 or more.
Example 2:
Input: s = "zzzzzyyyyy", words = ["zzyy","zy","zyy"] Output: 3
Constraints:
1 <= s.length, words.length <= 1001 <= words[i].length <= 100s and words[i] consist of lowercase letters.Problem summary: Sometimes people repeat letters to represent extra feeling. For example: "hello" -> "heeellooo" "hi" -> "hiiii" In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo". You are given a string s and an array of query strings words. A query word is stretchy if it can be made to be equal to s by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is three or more. For example, starting with "hello", we could do an extension on the group "o" to get "hellooo", but we cannot get "helloo" since the group "oo" has a size less than three. Also, we could do another extension like "ll" -> "lllll" to get "helllllooo". If s = "helllllooo", then the query word "hello" would be stretchy because of these two
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
"heeellooo" ["hello", "hi", "helo"]
"zzzzzyyyyy" ["zzyy","zy","zyy"]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #809: Expressive Words
class Solution {
public int expressiveWords(String s, String[] words) {
int ans = 0;
for (String t : words) {
if (check(s, t)) {
++ans;
}
}
return ans;
}
private boolean check(String s, String t) {
int m = s.length(), n = t.length();
if (n > m) {
return false;
}
int i = 0, j = 0;
while (i < m && j < n) {
if (s.charAt(i) != t.charAt(j)) {
return false;
}
int k = i;
while (k < m && s.charAt(k) == s.charAt(i)) {
++k;
}
int c1 = k - i;
i = k;
k = j;
while (k < n && t.charAt(k) == t.charAt(j)) {
++k;
}
int c2 = k - j;
j = k;
if (c1 < c2 || (c1 < 3 && c1 != c2)) {
return false;
}
}
return i == m && j == n;
}
}
// Accepted solution for LeetCode #809: Expressive Words
func expressiveWords(s string, words []string) (ans int) {
check := func(s, t string) bool {
m, n := len(s), len(t)
if n > m {
return false
}
i, j := 0, 0
for i < m && j < n {
if s[i] != t[j] {
return false
}
k := i
for k < m && s[k] == s[i] {
k++
}
c1 := k - i
i, k = k, j
for k < n && t[k] == t[j] {
k++
}
c2 := k - j
j = k
if c1 < c2 || (c1 != c2 && c1 < 3) {
return false
}
}
return i == m && j == n
}
for _, t := range words {
if check(s, t) {
ans++
}
}
return ans
}
# Accepted solution for LeetCode #809: Expressive Words
class Solution:
def expressiveWords(self, s: str, words: List[str]) -> int:
def check(s, t):
m, n = len(s), len(t)
if n > m:
return False
i = j = 0
while i < m and j < n:
if s[i] != t[j]:
return False
k = i
while k < m and s[k] == s[i]:
k += 1
c1 = k - i
i, k = k, j
while k < n and t[k] == t[j]:
k += 1
c2 = k - j
j = k
if c1 < c2 or (c1 < 3 and c1 != c2):
return False
return i == m and j == n
return sum(check(s, t) for t in words)
// Accepted solution for LeetCode #809: Expressive Words
struct Solution;
struct Word {
data: Vec<(char, usize)>,
}
impl Word {
fn new(s: String) -> Self {
let mut data = vec![];
let mut prev: Option<(char, usize)> = None;
for c in s.chars() {
if let Some(p) = prev {
if c == p.0 {
prev = Some((c, p.1 + 1));
} else {
data.push(p);
prev = Some((c, 1));
}
} else {
prev = Some((c, 1));
}
}
if let Some(p) = prev {
data.push(p);
}
Word { data }
}
fn stretchy(&self, word: &Word) -> bool {
let n = self.data.len();
let m = word.data.len();
if n != m {
return false;
}
for i in 0..n {
let p = self.data[i];
let q = word.data[i];
if p.0 != q.0 || p.1 > q.1 || (q.1 < 3 && p.1 != q.1) {
return false;
}
}
true
}
}
impl Solution {
fn expressive_words(s: String, words: Vec<String>) -> i32 {
let word = Word::new(s);
let words: Vec<Word> = words.into_iter().map(Word::new).collect();
words.iter().filter(|w| w.stretchy(&word)).count() as i32
}
}
#[test]
fn test() {
let s = "heeellooo".to_string();
let words = vec_string!["hello", "hi", "helo"];
let res = 1;
assert_eq!(Solution::expressive_words(s, words), res);
}
// Accepted solution for LeetCode #809: Expressive Words
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #809: Expressive Words
// class Solution {
// public int expressiveWords(String s, String[] words) {
// int ans = 0;
// for (String t : words) {
// if (check(s, t)) {
// ++ans;
// }
// }
// return ans;
// }
//
// private boolean check(String s, String t) {
// int m = s.length(), n = t.length();
// if (n > m) {
// return false;
// }
// int i = 0, j = 0;
// while (i < m && j < n) {
// if (s.charAt(i) != t.charAt(j)) {
// return false;
// }
// int k = i;
// while (k < m && s.charAt(k) == s.charAt(i)) {
// ++k;
// }
// int c1 = k - i;
// i = k;
// k = j;
// while (k < n && t.charAt(k) == t.charAt(j)) {
// ++k;
// }
// int c2 = k - j;
// j = k;
// if (c1 < c2 || (c1 < 3 && c1 != c2)) {
// return false;
// }
// }
// return i == m && j == n;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.