Guest User

Untitled

a guest
Feb 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. tch(text: String, pattern: String) -> Bool {
  2. var i: Int = 0
  3. var textIndex: Int = 0
  4. while i < pattern.count && textIndex < text.count {
  5. let currPat = Array(pattern)[i]
  6. var nextPat: String.Element = " "
  7. if i < pattern.count - 1 { nextPat = Array(pattern)[i+1] }
  8.  
  9.  
  10. if nextPat == "*" {
  11. var matchPat = currPat
  12. if currPat == "." { matchPat = Array(text)[textIndex] } // if ".*" pattern, save first item in sequence
  13. while textIndex < text.count && matchPat == Array(text)[textIndex] {
  14. textIndex += 1
  15. }
  16. i += 1 // extra increment to skip star
  17. } else if currPat != Array(text)[textIndex] && currPat != "." {
  18. return false
  19. } else {
  20. textIndex += 1
  21. }
  22.  
  23. i += 1
  24. }
  25.  
  26. // Handle case if pattern ends with "*" with element count of 0
  27. if i < pattern.count - 1 && Array(pattern)[i+1] == "*" { i += 2 }
  28.  
  29. return textIndex == text.count && i == pattern.count
  30. }
  31.  
  32. print(isMatch(text: "acdddd", pattern: "ab*c.*"))
Add Comment
Please, Sign In to add comment