Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func next_pattern_instruction(p string) (byte, byte) {
- ch := p[0]
- var pt byte = 1
- if len(p) > 1 && p[1] == '*' {
- pt = '*'
- }
- return ch, pt
- }
- func isMatch(s string, p string) bool {
- if len(p) == 0 {
- return len(s) == 0
- }
- ch, pt := next_pattern_instruction(p)
- if pt == 1 {
- // (1) a, 1 - s[0] == ch
- // (3) ., 1 - len(s) >= 1
- return len(s) >= 1 && (ch == '.' || s[0] == ch) && isMatch(s[1:], p[1:])
- } else {
- // (2) a, * - s[0] == ch || len(s) == 0
- // (4) ., * - len(s) >= 0
- // the pattern can happen 0 times regardless if there are characters or not
- if isMatch(s, p[2:]) {
- return true
- }
- // if there are characters we will try consuming them
- if len(s) >= 1 && (ch == '.' || s[0] == ch) {
- if isMatch(s[1:], p[2:]) {
- return true
- }
- return isMatch(s[1:], p)
- }
- return false
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement