Skip to content

Commit cbd6d08

Browse files
committed
Sync LeetCode submission Runtime - 0 ms (100.00%), Memory - 17.8 MB (6.08%)
1 parent bf2d188 commit cbd6d08

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<p>A string can be <strong>abbreviated</strong> by replacing any number of <strong>non-adjacent</strong>, <strong>non-empty</strong> substrings with their lengths. The lengths <strong>should not</strong> have leading zeros.</p>
2+
3+
<p>For example, a string such as <code>&quot;substitution&quot;</code> could be abbreviated as (but not limited to):</p>
4+
5+
<ul>
6+
<li><code>&quot;s10n&quot;</code> (<code>&quot;s <u>ubstitutio</u> n&quot;</code>)</li>
7+
<li><code>&quot;sub4u4&quot;</code> (<code>&quot;sub <u>stit</u> u <u>tion</u>&quot;</code>)</li>
8+
<li><code>&quot;12&quot;</code> (<code>&quot;<u>substitution</u>&quot;</code>)</li>
9+
<li><code>&quot;su3i1u2on&quot;</code> (<code>&quot;su <u>bst</u> i <u>t</u> u <u>ti</u> on&quot;</code>)</li>
10+
<li><code>&quot;substitution&quot;</code> (no substrings replaced)</li>
11+
</ul>
12+
13+
<p>The following are <strong>not valid</strong> abbreviations:</p>
14+
15+
<ul>
16+
<li><code>&quot;s55n&quot;</code> (<code>&quot;s <u>ubsti</u> <u>tutio</u> n&quot;</code>, the replaced substrings are adjacent)</li>
17+
<li><code>&quot;s010n&quot;</code> (has leading zeros)</li>
18+
<li><code>&quot;s0ubstitution&quot;</code> (replaces an empty substring)</li>
19+
</ul>
20+
21+
<p>Given a string <code>word</code> and an abbreviation <code>abbr</code>, return <em>whether the string <strong>matches</strong> the given abbreviation</em>.</p>
22+
23+
<p>A <strong>substring</strong> is a contiguous <strong>non-empty</strong> sequence of characters within a string.</p>
24+
25+
<p>&nbsp;</p>
26+
<p><strong class="example">Example 1:</strong></p>
27+
28+
<pre>
29+
<strong>Input:</strong> word = &quot;internationalization&quot;, abbr = &quot;i12iz4n&quot;
30+
<strong>Output:</strong> true
31+
<strong>Explanation:</strong> The word &quot;internationalization&quot; can be abbreviated as &quot;i12iz4n&quot; (&quot;i <u>nternational</u> iz <u>atio</u> n&quot;).
32+
</pre>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> word = &quot;apple&quot;, abbr = &quot;a2e&quot;
38+
<strong>Output:</strong> false
39+
<strong>Explanation:</strong> The word &quot;apple&quot; cannot be abbreviated as &quot;a2e&quot;.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>1 &lt;= word.length &lt;= 20</code></li>
47+
<li><code>word</code> consists of only lowercase English letters.</li>
48+
<li><code>1 &lt;= abbr.length &lt;= 10</code></li>
49+
<li><code>abbr</code> consists of lowercase English letters and digits.</li>
50+
<li>All the integers in <code>abbr</code> will fit in a 32-bit integer.</li>
51+
</ul>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Approach: Two Pointers
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def validWordAbbreviation(self, word: str, abbr: str) -> bool:
8+
i = j = 0 # i for word, j for abbr
9+
10+
while i < len(word) and j < len(abbr):
11+
if word[i] == abbr[j]:
12+
i += 1
13+
j += 1
14+
continue
15+
16+
if not abbr[j].isdigit():
17+
return False
18+
19+
if abbr[j] == '0':
20+
return False
21+
22+
# Get the complete number from abbr
23+
length = 0
24+
while j < len(abbr) and abbr[j].isdigit():
25+
length = length * 10 + int(abbr[j])
26+
j += 1
27+
28+
# Move word pointer by the length
29+
i += length
30+
31+
if i > len(word):
32+
return False
33+
34+
return i == len(word) and j == len(abbr)
35+

0 commit comments

Comments
 (0)