[Leetcode] 408. Valid Word Abbreviation 解题报告
2017-10-16 16:07
120 查看
题目:
Given a non-empty string
return whether the string matches with the given abbreviation.
A string such as
Notice that only the above abbreviations are valid abbreviations of the string
Any other string is not a valid abbreviation of
Note:
Assume
only lowercase letters and digits.
Example 1:
Example 2:
思路:
一道练手题目,注意边界控制即可。
代码:
class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
int index1 = 0, index2 = 0;
while(index1 < word.length() && index2 < abbr.length()){
if(isdigit(abbr[index2])) {
int value = abbr[index2++] - '0';
if (value == 0) {
return false;
}
while(index2 < abbr.length() && isdigit(abbr[index2])) {
value = 10 * value + abbr[index2++] - '0';
}
index1 += value;
}
else {
if(word[index1++] != abbr[index2++]) {
return false;
}
}
}
if(index1 != word.length() || index2 != abbr.length()) {
return false;
}
return true;
}
};
Given a non-empty string
sand 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
scontains only lowercase letters and
abbrcontains
only lowercase letters and digits.
Example 1:
Given s = "internationalization", abbr = "i12iz4n": Return true.
Example 2:
Given s = "apple", abbr = "a2e": Return false.
思路:
一道练手题目,注意边界控制即可。
代码:
class Solution {
public:
bool validWordAbbreviation(string word, string abbr) {
int index1 = 0, index2 = 0;
while(index1 < word.length() && index2 < abbr.length()){
if(isdigit(abbr[index2])) {
int value = abbr[index2++] - '0';
if (value == 0) {
return false;
}
while(index2 < abbr.length() && isdigit(abbr[index2])) {
value = 10 * value + abbr[index2++] - '0';
}
index1 += value;
}
else {
if(word[index1++] != abbr[index2++]) {
return false;
}
}
}
if(index1 != word.length() || index2 != abbr.length()) {
return false;
}
return true;
}
};
相关文章推荐
- Swap Nodes in Pairs LeetCode 解题报告
- [LeetCode 解题报告]014. Longest Common Prefix
- [leetcode]19. Remove Nth Node From End of List@Java解题报告
- 【LeetCode】Plus One 解题报告
- [leetcode] 348. Design Tic-Tac-Toe 解题报告
- [Leetcode] 422. Valid Word Square 解题报告
- [leetcode] 366. Find Leaves of Binary Tree 解题报告
- [LeetCode] Implement strStr() 解题报告
- [LeetCode] Longest Substring Without Repeating Characters 解题报告
- [LeetCode] Merge k Sorted Lists 解题报告
- [leetcode] 369. Plus One Linked List 解题报告
- 【LeetCode】TwoSum解题报告
- [LeetCode] Remove Duplicates from Sorted Array II 解题报告
- [Leetcode] 273. Integer to English Words 解题报告
- [LeetCode] Reverse Linked List II 解题报告
- [Leetcode] 52. N-Queens II 解题报告
- [Leetcode] 40. Combination Sum II 解题报告
- Leetcode Insertion Sort List 解题报告
- [LeetCode] Subsets 解题报告
- [LeetCode] Substring with Concatenation of All Words 解题报告