Expressive words problem case in JavaScript

Sometimes people repeat letters to represent extra feeling, such as "hello" ? "heeellooo", "hi" ? "hiiii". In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".

For some given string S, 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 3 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 size less than 3. 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.

Given a list of query words, we are required to return the number of words that are stretchy.

Problem Analysis

A word is stretchy if:

  • It has the same character sequence as the target string
  • Each character group can be extended to match the target, but only if the target group has 3 or more characters
  • If the target group has fewer than 3 characters, it must match exactly

Example

Let's solve this step by step:

const str = 'heeellooo';
const words = ["hello", "hi", "helo"];

const extraWords = (str, words) => {
    let count = 0;
    
    for (let w of words) {
        let i = 0; // pointer for str
        let j = 0; // pointer for word
        
        // Compare character by character
        for (; i  lenW && lenS  lenW && lenS 

1

How It Works

The algorithm processes each word by:

  1. Comparing character groups between the target string and query word
  2. For each matching character group, checking if extension rules are satisfied
  3. A group can be extended only if the target has 3+ characters of that type
  4. Counting words that successfully match after applying extension rules

Test with Different Examples

// Test case 1: "heeellooo" with ["hello", "hi", "helo"]
console.log("Test 1:", extraWords('heeellooo', ["hello", "hi", "helo"]));

// Test case 2: More examples
console.log("Test 2:", extraWords('zzzrrr', ["zr", "zzr", "zzzr"]));

// Test case 3: No stretchy words
console.log("Test 3:", extraWords('abc', ["ab", "abcd", "xyz"]));
Test 1: 1
Test 2: 1
Test 3: 0

Key Points

  • Extension requires at least 3 characters in the target group
  • Character order must match exactly
  • Groups with fewer than 3 characters cannot be extended
  • Both strings must be fully consumed for a valid match

Conclusion

The expressive words problem checks if query words can be "stretched" to match a target string by extending character groups of 3 or more. The solution uses two pointers to compare character groups and validate extension rules.

Updated on: 2026-03-15T23:19:00+05:30

261 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements