会员题-力扣408-有效单词缩写

有效单词缩写

字符串可以用 缩写 进行表示,缩写 的方法是将任意数量的 不相邻 的子字符串替换为相应子串的长度。例如,字符串 "substitution" 可以缩写为(不止这几种方法):

"s10n" ("s ubstitutio n")

"sub4u4" ("sub stit u tion")

"12" ("substitution")

"su3i1u2on" ("su bst i t u ti on")

"substitution" (没有替换子字符串)

下列是不合法的缩写:

"s55n" ("s ubsti tutio n",两处缩写相邻)

"s010n" (缩写存在前导零)

"s0ubstitution" (缩写是一个空字符串)

给你一个字符串单词 word 和一个缩写 abbr ,判断这个缩写是否可以是给定单词的缩写。

子字符串是字符串中连续的非空字符序列。

c 复制代码
class Solution {
public:
    bool validWordAbbreviation(string word, string abbr) {
        int abbrsize= abbr.size(),wordsize=word.size();
        //游标,记录当前判断的位置
        int abbrLen=0; 

        //num记录字母间的数字大小
        int num=0; 

        for (int i = 0; i < abbrsize; ++i){
            if (abbr[i]>='a' && abbr[i]<='z'){
                abbrLen+=num+1; //数字加字母的长度
                num=0;

                //越界或者单词对应位置不相同,直接返回false
                if (abbrLen>wordsize || abbr[i]!=word[abbrLen-1]) return false;
            }
            else{ 
                //遇到了前导0:当前缩写位置上的字符为0,并且num=0
                if (!num && abbr[i]=='0') return false;

                num=num*10+abbr[i]-'0'; //连续遇到两个数字,要拼接起来
            }
        }
        return abbrLen+num==wordsize;
    }
};
相关推荐
玩泥巴的3 小时前
搭建一套.net下能落地的飞书考勤系统
c#·.net·二次开发·飞书
唐宋元明清21886 小时前
.NET 本地Db数据库-技术方案选型
windows·c#
lindexi8 小时前
dotnet DirectX 通过可等待交换链降低输入渲染延迟
c#·directx·d2d·direct2d·vortice
ZPC82108 小时前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC82108 小时前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David8 小时前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
颜酱8 小时前
栈的经典应用:从基础到进阶,解决LeetCode高频栈类问题
javascript·后端·算法
多恩Stone8 小时前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
生信大杂烩8 小时前
癌症中的“细胞邻域“:解码肿瘤微环境的空间密码 ——Nature Cancer 综述解读
人工智能·算法
蜡笔小马9 小时前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost