倒序遍历字符串,若当前字符是d且前一个字符是c,或者当前字符是b且前一个字符是a则在字符串中消除这两个字符,直到字符串便遍历完毕,若字符串的长度等于0,则证明是有效配对,反之,则是无效配对
func isValidPairing(s string) bool {
// write code here
bytes := []byte(s)
n := len(bytes) - 1
for n > 0 {
switch bytes[n] {
case 'B':
if bytes[n-1] == 'A' {
goto NEXT
}
case 'D':
if bytes[n-1] == 'C' {
goto NEXT
}
}
n--
continue
NEXT:
if n+1 <= len(bytes)-1 {
bytes = append(bytes[:n-1], bytes[n+1:]...)
n--
} else {
bytes = append(bytes[:n-1])
n-=2
}
}
return len(bytes) == 0
}