Created
January 25, 2017 18:12
-
-
Save yehohanan7/00af32855929d29ed10bbb3293556648 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func matchPattern(str, pattern string, m, n int, mp map[rune]string) bool { | |
_ = "breakpoint" | |
if m == len(str) && n == len(pattern) { | |
return true | |
} | |
if m == len(str) || n == len(pattern) { | |
return false | |
} | |
for i := n; i < len(pattern); i++ { | |
p := rune(pattern[i]) | |
sub, ok := mp[p] | |
if !ok { | |
for j := 1; m+j < len(str); j++ { | |
mp[p] = str[m : m+j] | |
if matchPattern(str, pattern, m+j, n+1, mp) { | |
return true | |
} | |
delete(mp, p) | |
} | |
continue | |
} | |
if m+len(sub) <= len(str) && str[m:m+len(sub)] == sub { | |
return matchPattern(str, pattern, m+len(sub), n+1, mp) | |
} | |
return false | |
} | |
return false | |
} | |
func main() { | |
fmt.Println(matchPattern("maxmax", "abab", 0, 0, make(map[rune]string))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment