> For the complete documentation index, see [llms.txt](https://wentao-shao.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://wentao-shao.gitbook.io/leetcode/string/809.expressive-words.md).

# 809.Expressive-Words

## 809. Expressive Words

## 题目地址

<https://leetcode.com/problems/expressive-words/>

## 题目描述

```
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, return the number of words that are stretchy. 

Example:
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.

Notes:
0 <= len(S) <= 100.
0 <= len(words) <= 100.
0 <= len(words[i]) <= 100.
S and all words in words consist only of lowercase letters
```

## 代码

### Approach #1 Run Length Encoding

Time: O(QK) && Space: O(K)

where Q is the length of `words` (at least 1), and K is the maximum length of a word.)

```java
class Solution {
  public int expressiveWords(String S, String[] words) {
    RLE R = new RLE(S);
    int ans = 0;

    search: for (String word: words) {
      RLE R2 = new RLE(word);
      if (!R.key.equals(R2.key)) continue;
      for (int i = 0; i < R.counts.size(); i++) {
        int c1 = R.counts.get(i);
        int c2 = R2.counts.get(i);
        if (c1 < 3 && c1 != c2 || c1 < c3) {
          continue search;
        }
      }
      ans++;
    }

    return ans;
  }
}

class RLE {
  String key;
  List<Integer> counts;

  public RLE(String S) {
    StringBuilder sb = new StringBuilder();
    counts = new ArrayList();

    char[] ca = S.toCharArray();
    int N = ca.length;
    int prev = -1;
    for (int i = 0; i < N; i++) {
      if (i == N - 1 || ca[i] != ca[i+1]) {
        sb.append(ca[i]);
        counts.add(i - prev);
        prev = i;
      }
    }
    key = sb.toString();
  }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wentao-shao.gitbook.io/leetcode/string/809.expressive-words.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
