Skip to content

Commit 74957a0

Browse files
committed
408
1 parent 36307b5 commit 74957a0

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

README.org

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Leetcode solutions in C++, Java, Python
22

33
| Number | Title | Difficulty |
44
|--------+------------------------------------------------------------------------------------------------------------------------------------------------------+------------|
5+
| 408 | [[./src/408_valid_word_abbreviation][Valid Word Abbreviation]] | Medium |
56
| 407 | [[./src/407_trapping_rain_water_ii][Trapping Rain Water II]] | Hard |
67
| 406 | [[./src/406_queue_reconstruction_by_height][Queue Reconstruction by Height]] | Medium |
78
| 405 | [[./src/405_convert_a_number_to_hex][Convert a Number to Hexadecimal]] | Easy |
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
all:
2+
clang++ main.cpp -o main.o -std=c++11
3+
./main.o
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Problem
2+
===
3+
408. Valid Word Abbreviation
4+
Problem:
5+
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation.
6+
7+
A string such as "word" contains only the following valid abbreviations:
8+
9+
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
10+
Notice that only the above abbreviations are valid abbreviations of the string "word". Any other string is not a valid abbreviation of "word".
11+
12+
Note: Assume s contains only lowercase letters and abbr contains only lowercase letters and digits.
13+
14+
Example 1:
15+
16+
Given s = "internationalization", abbr = "i12iz4n":
17+
18+
Return true.
19+
Example 2:
20+
21+
Given s = "apple", abbr = "a2e":
22+
23+
Return false.
24+
25+
Solution
26+
===
27+
String iteration
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include <cstdlib>
2+
#include <iostream>
3+
#include <list>
4+
#include <map>
5+
#include <queue>
6+
#include <sstream>
7+
#include <string>
8+
#include <unordered_map>
9+
#include <unordered_set>
10+
#include <vector>
11+
12+
using namespace std;
13+
14+
15+
class Solution {
16+
public:
17+
bool validWordAbbreviation(string word, string abbr) {
18+
int i = 0, j = 0;
19+
int iter = 0;
20+
while (i < word.size() && j < abbr.size()) {
21+
if (abbr[j] >= '0' && abbr[j] <= '9') {
22+
if (abbr[j] == '0' && iter == 0) {
23+
return false;
24+
}
25+
while (j < abbr.size() && abbr[j] >= '0' && abbr[j] <= '9') {
26+
iter = 10 * iter + abbr[j] - '0';
27+
j++;
28+
}
29+
if (iter == 0) {
30+
return false;
31+
} else {
32+
i += iter;
33+
iter = 0;
34+
}
35+
if (iter != 0) {
36+
return false;
37+
}
38+
} else {
39+
if (word[i] == abbr[j]) {
40+
i++;
41+
j++;
42+
} else {
43+
return false;
44+
}
45+
}
46+
}
47+
return i == word.size() && j == abbr.size();
48+
}
49+
};
50+
51+
int main(int argc, const char * argv[]) {
52+
Solution sol;
53+
string s, abbr;
54+
55+
s = "internationalization", abbr = "i12iz4n";
56+
cout << sol.validWordAbbreviation(s, abbr) << endl;
57+
58+
s = "apple", abbr = "a2e";
59+
cout << sol.validWordAbbreviation(s, abbr) << endl;
60+
61+
s = "word", abbr = "4";
62+
cout << sol.validWordAbbreviation(s, abbr) << endl;
63+
64+
s = "word", abbr = "45";
65+
cout << sol.validWordAbbreviation(s, abbr) << endl;
66+
67+
s = "word", abbr = "wo0rd";
68+
cout << sol.validWordAbbreviation(s, abbr) << endl;
69+
70+
s = "word", abbr = "wo0rd";
71+
cout << sol.validWordAbbreviation(s, abbr) << endl;
72+
73+
s = "word", abbr = "wo01d";
74+
cout << sol.validWordAbbreviation(s, abbr) << endl;
75+
76+
s = "word", abbr = "word";
77+
cout << sol.validWordAbbreviation(s, abbr) << endl;
78+
79+
return 0;
80+
}

0 commit comments

Comments
 (0)