809. Expressive Words(字符串处理)

本文介绍了一种判断给定字符串是否可以通过扩展某些字符组来匹配另一字符串的方法。通过实现一个判断逻辑,该逻辑遍历两个字符串并比较字符及其重复次数,以此确定一个字符串是否能通过特定规则扩展成另一个字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

Sometimes people repeat letters to represent extra feeling, such as “hello” -> “heeellooo”, “hi” -> “hiiii”. Here, we have groups, of adjacent letters that are all the same character, and adjacent characters to the group are different. A group is extended if that group is length 3 or more, so “e” and “o” would be extended in the first example, and “i” would be extended in the second example. As another example, the groups of “abbcccaaaa” would be “a”, “bb”, “ccc”, and “aaaa”; and “ccc” and “aaaa” are the extended groups of that string.

For some given string S, a query word is stretchy if it can be made to be equal to S by extending some groups. Formally, we are allowed to repeatedly choose a group (as defined above) of characters c, and add some number of the same character c to it so that the length of the group is 3 or more. Note that we cannot extend a group of size one like “h” to a group of size two like “hh” - all extensions must leave the group extended - ie., at least 3 characters long.

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 extended.
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

解题思路

class Solution {
    public int expressiveWords(String S, String[] words) {
        int count = 0;
        for (String word : words) {
            if (isStretchy(word, S)) {
                count++;
            }
        }
        return count;
    }

    private boolean isStretchy(String original, String stretched) {// hello  heeellooo
        char[] origChars = original.toCharArray();       //origChars:   h e l l o
        char[] stretchedChars = stretched.toCharArray();   //stretched: h e e e l l o o o
        int i = 0;
        int j = 0;
        while (i < origChars.length && j < stretchedChars.length) {
            char currentChar = origChars[i]; //h,  //e
            if (currentChar != stretchedChars[j]) {//h==h // e==e
                return false;
            }

            int origCharCount = 0;//统计当前字符次数
            int stretchCharCount = 0;//统计当前字符在S={h e e e l l o o o}中次数

            while (i < origChars.length && origChars[i] == currentChar) {
                origCharCount++; //1               //1
                i++;//i=1,指向 h e l l o 中的 e     //i=2,指向 l
            }

            while (j < stretchedChars.length && stretchedChars[j] == currentChar) {
                stretchCharCount++; //1                                //3
                j++;//j=1,指向 stretched: h e e e l l o o o中的 e        //j=4,指向l
            }

            if (stretchCharCount > origCharCount && stretchCharCount < 3) {
                return false;
            }
            if (origCharCount > stretchCharCount) {
                return false;
            }
        }
        return i == origChars.length && j == stretchedChars.length; //i==5,j==9  hello可以扩展为heeellooo
    }

  /*  public static void main(String[] args) {
        Solution solution=new Solution();
        String S="heeellooo";
        String[]words={"hello","hi","helo"};
        int output=solution.expressiveWords(S,words);
        System.out.println(output);
    }*/
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值