Created
July 12, 2018 04:35
-
-
Save ekeitho/a59ca90dafd1800f94c542d928f19599 to your computer and use it in GitHub Desktop.
Regex . *
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
fun isMatch(word: String, pattern: String): Boolean { | |
if (pattern.length == 2 && pattern[0] == '.' && pattern[1] == '*') { | |
// recursively if we ever get to this, we know to return true regardless | |
return true | |
} | |
if (word.isEmpty() && pattern.isEmpty()) { | |
return true | |
} | |
if (word.isEmpty()) { | |
if (pattern.length > 1 && pattern[1] == '*') { | |
return isMatch(word, pattern.substring(2, pattern.length)) | |
} | |
return false | |
} | |
if (pattern.isEmpty()) { | |
return false | |
} | |
if (pattern.length > 1 && pattern[1] == '*') { | |
if (isMatch(word, pattern.substring(2, pattern.length)) == false) { | |
if (word[0] == pattern[0] || pattern[0] == '.') { | |
return isMatch(word.substring(1, word.length), pattern) | |
} | |
return false | |
} | |
return true | |
} | |
if (pattern[0] == '.' || pattern[0] == word[0]) { | |
return isMatch(word.substring(1, word.length), pattern.substring(1, pattern.length)) | |
} else { | |
return false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment