408. Valid Word Abbreviation

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.

A string such as "word" contains only the following valid abbreviations:

["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]

Notice that only the above abbreviations are valid abbreviations of the string "word". Any other string is not a valid abbreviation of "word".

Note:
Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.

Example 1:

Given s = "internationalization", abbr = "i12iz4n":

Return true.

Example 2:

Given s = "apple", abbr = "a2e":


Return false.

是时候该总结一下string类的问题了由Easy到hard这道题有几个可以学到的点:第一:在string中判断是否为数字我自己写代码的时候用的isdigit()函数,碰到多位数的时候从最后加好的string变为int,我们还可以用 if(str[i] >= 0 && str[i] <= 9)来判断,转化成int的时候也可以直接使用str[i] - ‘0‘来转换。第二:增加位数的时候可以用cnt = 10*cnt + str[i] - ‘0‘来更新 这种方法很常见

来看代码:
   bool validWordAbbreviation(string word, string abbr) {
        int pt1 = 0, pt2 = 0;
        while(pt1 < word.size() && pt2 < abbr.size())
        {
            if(‘0‘ <= abbr[pt2] && abbr[pt2] <= ‘9‘)
            {
                if(abbr[pt2] == ‘0‘)
                    return false;
                int value = 0;
                while(pt2 < abbr.size() && ‘0‘ <= abbr[pt2] && abbr[pt2] <= ‘9‘)
                {
                    value = value*10 + abbr[pt2]-‘0‘;
                    pt2++;
                }
                pt1 += value;
            }
            else
            {
                if(word[pt1++] != abbr[pt2++])
                    return false;
            }
        }
        return pt1 == word.size() && pt2 == abbr.size();
    }

 

				
时间: 2024-10-26 21:21:48

408. Valid Word Abbreviation的相关文章

408. Valid Word Abbreviation --字符串处理

Given s = "internationalization", abbr = "i12iz4n": Return true. abbr 里数字代表相应的字符数,问字符串是否相等 虽然是一个easy 的题但却有两个坑:1. abbr 结尾的地方是数字 例如: s= "internationalization" abbr= "i5a11o1" , 因此 return时得加上cout 来判断 index + Integer.va

[LeetCode] Valid Word Abbreviation 验证单词缩写

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

Valid Word Abbreviation Leetcode

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

LeetCode Valid Word Abbreviation

原题链接在这里:https://leetcode.com/problems/valid-word-abbreviation/#/description 题目: Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the followi

leetcode408 - Valid Word Abbreviation - easy

Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word", "1ord", "w1rd", &

[LeetCode] 527. Word Abbreviation 单词缩写

Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations for every word following rules below. Begin with the first character and then the number of characters abbreviated, which followed by the last charact

422. Valid Word Square

题目: Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(numRows, numColumns). Note: The number of words given is

LeetCode Valid Word Square

原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤ k < max(

[LeetCode] Valid Word Square 验证单词平方

Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a valid word square if the kth row and column read the exact same string, where 0 ≤k < max(numRows, numColumns). Note: The number of words given is at le