Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tch(text: String, pattern: String) -> Bool {
- var i: Int = 0
- var textIndex: Int = 0
- while i < pattern.count && textIndex < text.count {
- let currPat = Array(pattern)[i]
- var nextPat: String.Element = " "
- if i < pattern.count - 1 { nextPat = Array(pattern)[i+1] }
- if nextPat == "*" {
- var matchPat = currPat
- if currPat == "." { matchPat = Array(text)[textIndex] } // if ".*" pattern, save first item in sequence
- while textIndex < text.count && matchPat == Array(text)[textIndex] {
- textIndex += 1
- }
- i += 1 // extra increment to skip star
- } else if currPat != Array(text)[textIndex] && currPat != "." {
- return false
- } else {
- textIndex += 1
- }
- i += 1
- }
- // Handle case if pattern ends with "*" with element count of 0
- if i < pattern.count - 1 && Array(pattern)[i+1] == "*" { i += 2 }
- return textIndex == text.count && i == pattern.count
- }
- print(isMatch(text: "acdddd", pattern: "ab*c.*"))
Add Comment
Please, Sign In to add comment