Advertisement
seulunga

Untitled

Aug 21st, 2020
1,447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.84 KB | None | 0 0
  1. func next_pattern_instruction(p string) (byte, byte) {
  2.     ch := p[0]
  3.     var pt byte = 1
  4.     if len(p) > 1 && p[1] == '*' {
  5.         pt = '*'
  6.     }
  7.  
  8.     return ch, pt
  9. }
  10.  
  11. func isMatch(s string, p string) bool {
  12.     if len(p) == 0 {
  13.         return len(s) == 0
  14.     }
  15.  
  16.     ch, pt := next_pattern_instruction(p)
  17.  
  18.     if pt == 1 {
  19.         // (1) a, 1 - s[0] == ch
  20.         // (3) ., 1 - len(s) >= 1
  21.         return len(s) >= 1 && (ch == '.' || s[0] == ch) && isMatch(s[1:], p[1:])
  22.     } else {
  23.         // (2) a, * - s[0] == ch || len(s) == 0
  24.         // (4) ., * - len(s) >= 0
  25.  
  26.         // the pattern can happen 0 times regardless if there are characters or not
  27.         if isMatch(s, p[2:]) {
  28.             return true
  29.         }
  30.         // if there are characters we will try consuming them
  31.         if len(s) >= 1 && (ch == '.' || s[0] == ch) {
  32.             if isMatch(s[1:], p[2:]) {
  33.                 return true
  34.             }
  35.             return isMatch(s[1:], p)
  36.         }
  37.         return false
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement