Class Solution

java.lang.Object
g0801_0900.s0809_expressive_words.Solution

public class Solution extends Object
809 - Expressive Words.<p>Medium</p> <p>Sometimes people repeat letters to represent extra feeling. For example:</p> <ul> <li><code>&quot;hello&quot; -> &quot;heeellooo&quot;</code></li> <li><code>&quot;hi&quot; -> &quot;hiiii&quot;</code></li> </ul> <p>In these strings like <code>&quot;heeellooo&quot;</code>, we have groups of adjacent letters that are all the same: <code>&quot;h&quot;</code>, <code>&quot;eee&quot;</code>, <code>&quot;ll&quot;</code>, <code>&quot;ooo&quot;</code>.</p> <p>You are given a string <code>s</code> and an array of query strings <code>words</code>. A query word is <strong>stretchy</strong> if it can be made to be equal to <code>s</code> by any number of applications of the following extension operation: choose a group consisting of characters <code>c</code>, and add some number of characters <code>c</code> to the group so that the size of the group is <strong>three or more</strong>.</p> <ul> <li>For example, starting with <code>&quot;hello&quot;</code>, we could do an extension on the group <code>&quot;o&quot;</code> to get <code>&quot;hellooo&quot;</code>, but we cannot get <code>&quot;helloo&quot;</code> since the group <code>&quot;oo&quot;</code> has a size less than three. Also, we could do another extension like <code>&quot;ll&quot; -> &quot;lllll&quot;</code> to get <code>&quot;helllllooo&quot;</code>. If <code>s = &quot;helllllooo&quot;</code>, then the query word <code>&quot;hello&quot;</code> would be <strong>stretchy</strong> because of these two extension operations: <code>query = &quot;hello&quot; -> &quot;hellooo&quot; -> &quot;helllllooo&quot; = s</code>.</li> </ul> <p>Return <em>the number of query strings that are <strong>stretchy</strong></em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;heeellooo&rdquo;, words = [&ldquo;hello&rdquo;, &ldquo;hi&rdquo;, &ldquo;helo&rdquo;]</p> <p><strong>Output:</strong> 1</p> <p><strong>Explanation:</strong></p> <p>We can extend &ldquo;e&rdquo; and &ldquo;o&rdquo; in the word &ldquo;hello&rdquo; to get &ldquo;heeellooo&rdquo;.</p> <p>We can&rsquo;t extend &ldquo;helo&rdquo; to get &ldquo;heeellooo&rdquo; because the group &ldquo;ll&rdquo; is not size 3 or more.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;zzzzzyyyyy&rdquo;, words = [&ldquo;zzyy&rdquo;,&ldquo;zy&rdquo;,&ldquo;zyy&rdquo;]</p> <p><strong>Output:</strong> 3</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length, words.length <= 100</code></li> <li><code>1 <= words[i].length <= 100</code></li> <li><code>s</code> and <code>words[i]</code> consist of lowercase letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • expressiveWords

      public int expressiveWords(String s, String[] words)