Skip to content

Commit eff753a

Browse files
committed
[String] Add a solution to Valid Word Abbreviation
1 parent 43e2df4 commit eff753a

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
![Leetcode](./logo.png?style=centerme)
55

66
## Progress
7-
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 320 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
7+
[Problem Status](#problem-status) shows the latest progress to all 1000+ questions. Currently we have 321 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them.
88

99
## Contributors
1010

@@ -104,6 +104,7 @@
104104
[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)| [Swift](./String/FizzBuzz.swift)| Easy| O(n)| O(1)|
105105
[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)| [Swift](./String/FirstUniqueCharacterInString.swift)| Easy| O(n)| O(1)|
106106
[Keyboard Row](https://leetcode.com/problems/keyboard-row/)| [Swift](./String/KeyboardRow.swift)| Easy| O(nm)| O(n)|
107+
[Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/)| [Swift](./String/ValidWordAbbreviation.swift)| Easy| O(n)| O(n)|
107108
[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)| [Swift](./String/ValidPalindrome.swift)| Easy| O(n)| O(n)|
108109
[Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/)| [Swift](./String/ValidPalindromeII.swift)| Easy| O(n)| O(n)|
109110
[Detect Capital](https://leetcode.com/problems/detect-capital/)| [Swift](./String/DetectCapital.swift)| Easy| O(n)| O(1)|

String/ValidWordAbbreviation.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/valid-word-abbreviation/
3+
* Primary idea: Go through both string and compare characters or skip by the number
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class ValidWordAbbreviation {
10+
func validWordAbbreviation(_ word: String, _ abbr: String) -> Bool {
11+
var i = 0, j = 0
12+
let word = Array(word), abbr = Array(abbr)
13+
14+
while i < word.count && j < abbr.count {
15+
if abbr[j].isNumber {
16+
// edge case: "abbc" vs. "a02c"
17+
if abbr[j] == "0" {
18+
return false
19+
}
20+
21+
let start = j
22+
23+
while j < abbr.count && abbr[j].isNumber {
24+
j += 1
25+
}
26+
27+
let end = j - 1
28+
29+
i += Int(String(abbr[start...end]))!
30+
} else {
31+
if abbr[j] != word[i] {
32+
return false
33+
} else {
34+
i += 1
35+
j += 1
36+
}
37+
}
38+
}
39+
40+
// edge case: "hi" vs. "hi1"
41+
return i == word.count && j == abbr.count
42+
}
43+
}

0 commit comments

Comments
 (0)