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所有的指针都走到了最后
    }
}
相关推荐
coffee_baby10 分钟前
化繁为简:中介者模式如何管理复杂对象交互
java·spring boot·microsoft·交互·中介者模式
ღ᭄ꦿ࿐Never say never꧂14 分钟前
微服务架构中的负载均衡与服务注册中心(Nacos)
java·spring boot·后端·spring cloud·微服务·架构·负载均衡
所待.38315 分钟前
小小扑克牌算法
java·算法
.生产的驴23 分钟前
SpringBoot 消息队列RabbitMQ 消息确认机制确保消息发送成功和失败 生产者确认
java·javascript·spring boot·后端·rabbitmq·负载均衡·java-rabbitmq
.生产的驴23 分钟前
SpringBoot 消息队列RabbitMQ在代码中声明 交换机 与 队列使用注解创建
java·spring boot·分布式·servlet·kafka·rabbitmq·java-rabbitmq
idealzouhu37 分钟前
Java 并发编程 —— AQS 抽象队列同步器
java·开发语言
听封41 分钟前
Thymeleaf 的创建
java·spring boot·spring·maven
写bug写bug1 小时前
6 种服务限流的实现方式
java·后端·微服务
楠枬1 小时前
双指针算法
java·算法·leetcode