Created
July 2, 2017 21:02
-
-
Save jianminchen/2238153f235d2e8bd5dc98b998fd0a4e to your computer and use it in GitHub Desktop.
String matching, . matches any char, * means to repeat zero or at least one times.
This file contains 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
def isMatch(text, pattern) | |
if pattern.length == 1 && text.length == 0 | |
return false # "", "a" return false | |
end | |
if pattern.length == 1 && text.length == 1 && pattern == '.' | |
return true # "a" "." return true | |
end | |
#"" "a*" | |
if pattern.length == 1 | |
return text == pattern # "a" "a" pattern . | |
end | |
test_pattern = pattern[0] # | |
star = true if pattern[1] == '*' | |
match = false | |
if star | |
match ||= isMatch(text, pattern.slice(2, pattern.length)) | |
match ||= isMatch(text.slice(1, text.length), pattern) if text[0] == test_pattern | |
elsif text[0] == test_pattern | |
match ||= isMatch(text.slice(1, text.length), pattern.slice(1, pattern.length)) | |
end | |
return match | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment