408. Valid Word Abbreviation

A string can be abbreviated by replacing any number of non-adjacent , non-empty substrings with their lengths. The lengths should not have leading zeros.

For example, a string such as "substitution" could be abbreviated as (but not limited to):

  • "s10n" ("s ++ubstitutio++ n")
  • "sub4u4" ("sub ++stit++u++tion++")
  • "12" ("++substitution++")
  • "su3i1u2on" ("su ++bst++i++t++u++ti++ on")
  • "substitution" (no substrings replaced)

The following are not valid abbreviations:

  • "s55n" ("s ++ubsti++ ++tutio++ n", the replaced substrings are adjacent)
  • "s010n" (has leading zeros)
  • "s0ubstitution" (replaces an empty substring)

Given a string word and an abbreviation abbr, return whether the string matches the given abbreviation.

A substring is a contiguous non-empty sequence of characters within a string.

Example 1:

复制代码
Input: word = "internationalization", abbr = "i12iz4n"
Output: true
Explanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").

Example 2:

复制代码
Input: word = "apple", abbr = "a2e"
Output: false
Explanation: The word "apple" cannot be abbreviated as "a2e".

Constraints:

  • 1 <= word.length <= 20
  • word consists of only lowercase English letters.
  • 1 <= abbr.length <= 10
  • abbr consists of lowercase English letters and digits.
  • All the integers in abbr will fit in a 32-bit integer.
java 复制代码
class Solution {
    public boolean validWordAbbreviation(String word, String abbr) {
        int i = 0;
        int j = 0;

        while(i<word.length() && j<abbr.length()){
            char a = abbr.charAt(j);
            if(Character.isDigit(a)){
                if(a == '0'){
                    return false;
                }
                int number = a - '0'; //这里的number每次都要重新生成一下
                while(j+1 < abbr.length() && Character.isDigit(abbr.charAt(j+1))){ 
                    number = number*10 + (abbr.charAt(j+1) - '0');
                    j++;
                }
                i += number;
                j++;

            }else{
                if(abbr.charAt(j) != word.charAt(i)){
                    return false;
                }else{
                    i++;
                    j++;
                }
            }
        }
        return i == word.length() && j == abbr.length(); //最后不是无脑返回true,要make sure所有的指针都走到了最后
    }
}
相关推荐
独自破碎E2 分钟前
BISHI75 阶幂
android·java·开发语言
红中️2 分钟前
Tomcat
java·tomcat
Charlie_lll4 分钟前
力扣解题-无重复字符的最长子串
后端·算法·leetcode
爱学习的小可爱卢6 分钟前
JavaSE基础-Java异常体系:Bug定位终极指南
java·bug·javase
甲枫叶7 分钟前
【claude+weelinking产品经理系列15】UI/UX 打磨——产品经理的审美终于能自己实现
java·人工智能·python·ui·产品经理·ai编程·ux
zihan03219 分钟前
将若依(RuoYi)框架从适配 Spring Boot 2 的版本升级到 Spring Boot 3
java·spring boot·github·若依框架
@insist12310 分钟前
软考-软件设计师-数据表示核心考点详解:从进制转换到 IEEE 754 标准
java·数据结构·算法
柒.梧.12 分钟前
Java拷贝精讲:彻底分清浅拷贝与深拷贝
java·开发语言·python
七夜zippoe15 分钟前
微服务架构下Spring Session与Redis分布式会话实战全解析
java·redis·maven·spring session·分布式会话
vx_Biye_Design2 小时前
【关注可免费领取源码】云计算及其应用网络教学系统--毕设附源码35183
java·spring·spring cloud·servlet·eclipse·云计算·课程设计