-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1003-CheckIfWordIsValidAfterSubstitutions.go
More file actions
98 lines (86 loc) · 2.71 KB
/
1003-CheckIfWordIsValidAfterSubstitutions.go
File metadata and controls
98 lines (86 loc) · 2.71 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
package main
// 1003. Check If Word Is Valid After Substitutions
// Given a string s, determine if it is valid.
// A string s is valid if, starting with an empty string t = "",
// you can transform t into s after performing the following operation any number of times:
// Insert string "abc" into any position in t.
// More formally, t becomes tleft + "abc" + tright, where t == tleft + tright.
// Note that tleft and tright may be empty.
// Return true if s is a valid string, otherwise, return false.
// Example 1:
// Input: s = "aabcbc"
// Output: true
// Explanation:
// "" -> "abc" -> "aabcbc"
// Thus, "aabcbc" is valid.
// Example 2:
// Input: s = "abcabcababcc"
// Output: true
// Explanation:
// "" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
// Thus, "abcabcababcc" is valid.
// Example 3:
// Input: s = "abccba"
// Output: false
// Explanation: It is impossible to get "abccba" using the operation.
// Constraints:
// 1 <= s.length <= 2 * 10^4
// s consists of letters 'a', 'b', and 'c'
import "fmt"
func isValid(s string) bool {
isDeleted := true
for isDeleted {
isDeleted = false
for i := 0; i < len(s) - 2; i++ {
if s[i: i + 3] == "abc" { // 如果是 abc 则做截取操作
s = s[:i] + s[i + 3:]
isDeleted = true
break
}
}
}
return len(s) == 0
}
// stack abc 看成一组,遇到c 时需要从栈依次 pop 出b a
func isValid1(s string) bool {
stack := []byte{}
for _, v := range s {
if v == 'c' { // pop 两个值,且依次为 b a
if len(stack) < 2 {
return false
}
a, b := stack[len(stack)-2], stack[len(stack)-1] // pop twice
stack = stack[:len(stack)-2]
if a != 'a' || b != 'b' {
return false
}
} else {
stack = append(stack, byte(v))
}
}
return len(stack) == 0
}
func main() {
// Example 1:
// Input: s = "aabcbc"
// Output: true
// Explanation:
// "" -> "abc" -> "aabcbc"
// Thus, "aabcbc" is valid.
fmt.Println(isValid("aabcbc")) // true
// Example 2:
// Input: s = "abcabcababcc"
// Output: true
// Explanation:
// "" -> "abc" -> "abcabc" -> "abcabcabc" -> "abcabcababcc"
// Thus, "abcabcababcc" is valid.
fmt.Println(isValid("abcabcababcc")) // true
// Example 3:
// Input: s = "abccba"
// Output: false
// Explanation: It is impossible to get "abccba" using the operation.
fmt.Println(isValid("abccba")) // false
fmt.Println(isValid1("aabcbc")) // true
fmt.Println(isValid1("abcabcababcc")) // true
fmt.Println(isValid1("abccba")) // false
}