【每日一题Day37】LC809情感丰富的文字 | 双指针 模拟

简介: 当word的长度大于s的长度时,那么一定不可以进行转化

情感丰富的文字【LC809】


Sometimes people repeat letters to represent extra feeling. For example:


  • "hello" -> "heeellooo"


  • "hi" -> "hiiii"


In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo".


You are given a string s and an array of query strings words. 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 three 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 a size less than three. 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.


Return the number of query strings that are stretchy.


原来来来外国人人人人都是这样说话话话话的吗吗吗吗

全校静默的第一天天天天


  • 思路:统计words中可以转化为s的个数,转化方式为当s[i,rs] == word[j,rw]时,可以扩展word中字符使其与s相等,条件为rs−i+1≥3。因此遍历words中的每个word,设置两个指针分别指向s和word的首部,如果字符相等,那么寻找该字符在s和word中的右边界,并判断是否需要进行扩展,以及能否进行扩展;如果字符不相等,那么一定不能进行转化,直接break


。rs−i+1>rw−j+1时,需要进行转化


  • rs−i+1≥3时,不可以进行转化


  • rs−i+1<3时,可以进行转化


。rs−i+1<rw−j+1时,不可以进行转化,break


。rs−i+1==rw−j+1时,不需要进行转化,继续判断


  • 实现:


当word的长度大于s的长度时,那么一定不可以进行转化


class Solution {
    public int expressiveWords(String s, String[] words) {
        int len = s.length();
        int count = 0;
        for (String word : words){
            int lenWord = word.length();
            if (lenWord > len){
                break;
            }
            int i = 0, j = 0;
            while (i < len && j < lenWord && s.charAt(i) == word.charAt(j)){             
                int rightS = getRightBorder(s,i);
                int rightWord = getRightBorder(word,j);
                if (rightS - i + 1 < rightWord - j + 1){
                    break;
                }else if (rightS - i + 1 > rightWord - j + 1 && rightS - i + 1 < 3){
                    break;
                }
                // rightS - i + 1 == rightWord - j + 1 
                // rightS - i + 1 > rightWord - j + 1 && rightS - i + 1 >= 3
                i = rightS + 1;
                j = rightWord + 1;
                if (i == len && j == lenWord){
                    count++;
                }           
            }
        }
        return count;
    }
    public int getRightBorder(String s,int left){
        int right = left;
        char c = s.charAt(left);
        while (right + 1 < s.length() && s.charAt(right + 1) == c){
            right++;
        }
        return right;
    }
}


目录
相关文章
|
15天前
|
存储 弹性计算 人工智能
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
2025年9月24日,阿里云弹性计算团队多位产品、技术专家及服务器团队技术专家共同在【2025云栖大会】现场带来了《通用计算产品发布与行业实践》的专场论坛,本论坛聚焦弹性计算多款通用算力产品发布。同时,ECS云服务器安全能力、资源售卖模式、计算AI助手等用户体验关键环节也宣布升级,让用云更简单、更智能。海尔三翼鸟云服务负责人刘建锋先生作为特邀嘉宾,莅临现场分享了关于阿里云ECS g9i推动AIoT平台的场景落地实践。
【2025云栖精华内容】 打造持续领先,全球覆盖的澎湃算力底座——通用计算产品发布与行业实践专场回顾
|
7天前
|
云安全 人工智能 安全
Dify平台集成阿里云AI安全护栏,构建AI Runtime安全防线
阿里云 AI 安全护栏加入Dify平台,打造可信赖的 AI
|
10天前
|
人工智能 运维 Java
Spring AI Alibaba Admin 开源!以数据为中心的 Agent 开发平台
Spring AI Alibaba Admin 正式发布!一站式实现 Prompt 管理、动态热更新、评测集构建、自动化评估与全链路可观测,助力企业高效构建可信赖的 AI Agent 应用。开源共建,现已上线!
931 29
|
9天前
|
机器学习/深度学习 人工智能 搜索推荐
万字长文深度解析最新Deep Research技术:前沿架构、核心技术与未来展望
近期发生了什么自 2025 年 2 月 OpenAI 正式发布Deep Research以来,深度研究/深度搜索(Deep Research / Deep Search)正在成为信息检索与知识工作的全新范式:系统以多步推理驱动大规模联网检索、跨源证据。
672 52
|
3天前
|
监控 BI 数据库
打工人救星!来看看这两家企业如何用Quick BI让业务更高效
Quick BI专业版监控告警助力企业高效运作,通过灵活配置规则与多渠道推送,让数据异常早发现、快响应,推动业务敏捷决策与持续增长。
打工人救星!来看看这两家企业如何用Quick BI让业务更高效
|
7天前
|
文字识别 测试技术 开发者
Qwen3-VL新成员 2B、32B来啦!更适合开发者体质
Qwen3-VL家族重磅推出2B与32B双版本,轻量高效与超强推理兼备,一模型通吃多模态与纯文本任务!
594 11