码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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.
    1. class Solution {
    2. public boolean validWordAbbreviation(String word, String abbr) {
    3. int i = 0;
    4. int j = 0;
    5. while(i
    6. char a = abbr.charAt(j);
    7. if(Character.isDigit(a)){
    8. if(a == '0'){
    9. return false;
    10. }
    11. int number = a - '0'; //这里的number每次都要重新生成一下
    12. while(j+1 < abbr.length() && Character.isDigit(abbr.charAt(j+1))){
    13. number = number*10 + (abbr.charAt(j+1) - '0');
    14. j++;
    15. }
    16. i += number;
    17. j++;
    18. }else{
    19. if(abbr.charAt(j) != word.charAt(i)){
    20. return false;
    21. }else{
    22. i++;
    23. j++;
    24. }
    25. }
    26. }
    27. return i == word.length() && j == abbr.length(); //最后不是无脑返回true,要make sure所有的指针都走到了最后
    28. }
    29. }

  • 相关阅读:
    全网最全谷粒商城记录_08、环境-linux安装docker
    Go 之为什么 rune 是 int32 的别名而不是 uint32 的别名
    【C语言】进阶——字符串和内存函数
    【触想智能】工业触摸显示器在户外使用需要注意哪些问题?
    R语言 山峦图
    CSI2与CDPHY学习
    【logrotate】linux定时文件切割(解决openresty单个日志文件过大问题)
    Git 版本控制:构建高效协作和开发流程的最佳实践
    面试面经|Java面试MySQL面试题
    拼搏半个月,刷了 571道Java高频面试题喜提 offer
  • 原文地址:https://blog.csdn.net/lilibaobao89/article/details/134226805
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号