Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created July 2, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/2238153f235d2e8bd5dc98b998fd0a4e to your computer and use it in GitHub Desktop.
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.
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