情感丰富的文字

题目

有时候人们会用重复写一些字母来表示额外的感受,比如 "hello" -> "heeellooo", "hi" -> "hiii"。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h", "eee", "ll", "ooo"。

对于一个给定的字符串 S ,如果另一个单词能够通过将一些字母组扩张从而使其和 S 相同,我们将这个单词定义为可扩张的(stretchy)。扩张操作定义如下:选择一个字母组(包含字母 c ),然后往其中添加相同的字母 c 使其长度达到 3 或以上。

例如,以 "hello" 为例,我们可以对字母组 "o" 扩张得到 "hellooo",但是无法以同样的方法得到 "helloo" 因为字母组 "oo" 长度小于 3。此外,我们可以进行另一种扩张 "ll" -> "lll" 以获得 "helllooo"。如果 s = "helllooo",那么查询词 "hello" 是可扩张的,因为可以对它执行这两种扩张操作使得 query = "hello" -> "hellooo" -> "helllooo" = s。

输入一组查询单词,输出其中可扩张的单词数量。

示例:

输入:
s = "heeellooo"
words = ["hello", "hi", "helo"]
输出:1
解释:
我们能通过扩张 "hello" 的 "e" 和 "o" 来得到 "heeellooo"。
我们不能通过扩张 "helo" 来得到 "heeellooo" 因为 "ll" 的长度小于 3 。

提示:

1 <= s.length, words.length <= 100
1 <= words[i].length <= 100
s 和所有在 words 中的单词都只由小写字母组成。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/expressive-words
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法:双指针

  • 需要注意原题目有问题,“ll”扩展“lll”是可以的
public static int expressiveWords(String s, String[] words) {
        int count = 0;
        for (String word :words
             ) {
            int sIndex1=0;
            boolean isExpressiveWord=true;
            for (int wordIndex1 = 0; wordIndex1 < word.length(); wordIndex1++) {
                int wordIndex2=wordIndex1;
                int wordRepeatCount=1;
                if(sIndex1>=s.length())
                {
                    isExpressiveWord=false;
                    break;
                }
                if(s.charAt(sIndex1)!=word.charAt(wordIndex1))
                {
                    isExpressiveWord=false;
                    break;
                }
                while(true)
                {
                    wordIndex2++;
                    if(wordIndex2 >= word.length())
                    {
                        break;
                    }
                    if(word.charAt(wordIndex1)==word.charAt(wordIndex2))
                    {
                        wordRepeatCount++;
                        wordIndex1=wordIndex2;
                    }
                    else
                    {
                        break;
                    }
                }
                int sRepeatCount=1;
                int sIndex2=sIndex1;
                while(true)
                {
                    sIndex2++;
                    if(sIndex2 >= s.length())
                    {
                        break;
                    }
                    if(s.charAt(sIndex1)==s.charAt(sIndex2))
                    {
                        sRepeatCount++;
                        sIndex1=sIndex2;
                    }
                    else
                    {
                        break;
                    }
                }
                sIndex1++;
                if(sRepeatCount==wordRepeatCount)
                {
                    continue;
                }else if(sRepeatCount<wordRepeatCount)
                {
                    isExpressiveWord=false;
                    break;
                }else{//sRepeatCount>wordRepeatCount
                    if(sRepeatCount==2)
                    {
                        isExpressiveWord=false;
                        break;
                    }
                }
            }
            if(isExpressiveWord && sIndex1>=s.length())
            {
                count++;
            }
        }
        return count;
    }
posted @ 2022-11-25 17:06  花茶冰糖  阅读(16)  评论(0编辑  收藏  举报