-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path408-ValidWordAbbreviation.go
More file actions
107 lines (95 loc) · 3.41 KB
/
408-ValidWordAbbreviation.go
File metadata and controls
107 lines (95 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
// 408. Valid Word Abbreviation
// A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths.
// The lengths should not have leading zeros.
// For example, a string such as "substitution" could be abbreviated as (but not limited to):
// "s10n" ("s ubstitutio n")
// "sub4u4" ("sub stit u tion")
// "12" ("substitution")
// "su3i1u2on" ("su bst i t u ti on")
// "substitution" (no substrings replaced)
// The following are not valid abbreviations:
// "s55n" ("s ubsti tutio n", the replaced substrings are adjacent)
// "s010n" (has leading zeros)
// "s0ubstitution" (replaces an empty substring)
// Given a string word and an abbreviation abbr, return whether the string matches the given abbreviation.
// A substring is a contiguous non-empty sequence of characters within a string.
// Example 1:
// Input: word = "internationalization", abbr = "i12iz4n"
// Output: true
// Explanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").
// Example 2:
// Input: word = "apple", abbr = "a2e"
// Output: false
// Explanation: The word "apple" cannot be abbreviated as "a2e".
// Constraints:
// 1 <= word.length <= 20
// word consists of only lowercase English letters.
// 1 <= abbr.length <= 10
// abbr consists of lowercase English letters and digits.
// All the integers in abbr will fit in a 32-bit integer.
import "fmt"
// func validWordAbbreviation(word string, abbr string) bool {
// index, num := 0, 0
// for i := 0; i < len(abbr); i++ {
// if abbr[i] >= '0' && abbr[i] <= '9' { // 处理数字 "s010n" (has leading zeros) 的情况
// n := int(abbr[i] - '0')
// if num == 0 && n == 0 { // 处理
// return false
// }
// num = num * 10 + n
// } else {
// if num != 0 {
// index += num
// num = 0 // 重新置 0
// }
// fmt.Printf("word[%v] = %v, abbr[%v]= %v\n",index, word[index], i, abbr[i])
// if word[index] != abbr[i] {
// return false
// }
// index++
// }
// }
// return true
// }
func validWordAbbreviation(word string, abbr string) bool {
m, n :=len(word), len(abbr)
if m==0 && n == 0{
return true
}
if m==0 || n == 0 {
return false
}
if isAlpha(abbr[0]) {
if abbr[0] != word[0] {
return false
}
return validWordAbbreviation(word[1:], abbr[1:])
}
if abbr[0] == '0' {
return false
}
num, i := 0, 0
for ; i < n && !isAlpha(abbr[i]); i++ {
num = num * 10 + int(abbr[i] - '0')
}
if num > m {
return false
}
return validWordAbbreviation(word[num:],abbr[i:])
}
func isAlpha(ch byte)bool{
return 'a' <= ch && ch <= 'z'
}
func main() {
// Example 1:
// Input: word = "internationalization", abbr = "i12iz4n"
// Output: true
// Explanation: The word "internationalization" can be abbreviated as "i12iz4n" ("i nternational iz atio n").
fmt.Println(validWordAbbreviation("internationalization","i12iz4n")) // true
// Example 2:
// Input: word = "apple", abbr = "a2e"
// Output: false
// Explanation: The word "apple" cannot be abbreviated as "a2e".
fmt.Println(validWordAbbreviation("apple","a2e")) // false
}