ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 408. Valid Word Abbreviation

2022-07-07 13:38:55  阅读:170  来源: 互联网

标签:缩写 word num Abbreviation Valid abbr substitution 字符串 Word


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.

有效单词缩写。

字符串可以用 缩写 进行表示,缩写 的方法是将任意数量的 不相邻 的子字符串替换为相应子串的长度。例如,字符串 "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 ,判断这个缩写是否可以是给定单词的缩写。

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/valid-word-abbreviation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这是一道字符串的基础题,不难,注意细节。

思路是双指针,两个指针 i, j 分别指向 word 和 abbr。如果两边指向的字母相同,则分别往前走一步;如果 abbr 这一边指向了一个不是 0 开头的数字,则我们算一下这个数字 num 到底是多少,然后让 i 指针往前走 num 步。如果顺利,最后两个指针应该是同时到达 word 和 abbr 的尾部。

时间O(n)

空间O(1)

Java实现

 1 class Solution {
 2     public boolean validWordAbbreviation(String word, String abbr) {
 3         // corner case
 4         if (word == null || abbr == null) {
 5             return false;
 6         }
 7         
 8         // normal case
 9         int i = 0;
10         int j = 0;
11         while (i < word.length() && j < abbr.length()) {
12             if (word.charAt(i) == abbr.charAt(j)) {
13                 i++;
14                 j++;
15             } else if (Character.isDigit(abbr.charAt(j)) && abbr.charAt(j) != '0') {
16                 int num = 0;
17                 while (j < abbr.length() && Character.isDigit(abbr.charAt(j))) {
18                     num = num * 10 + abbr.charAt(j) - '0';
19                     j++;
20                 }
21                 i += num;
22             } else {
23                 return false;
24             }
25         }
26         return i == word.length() && j == abbr.length();
27     }
28 }

 

LeetCode 题目总结

标签:缩写,word,num,Abbreviation,Valid,abbr,substitution,字符串,Word
来源: https://www.cnblogs.com/cnoodle/p/16454352.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有