Advertisement
Alexxik

Untitled

Sep 12th, 2023 (edited)
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.67 KB | None | 0 0
  1. // MARK: - 125 Valid Palindrome
  2.  
  3. func isPalindrome(_ s: String) -> Bool {
  4.     let s: [Character] = Array(s)
  5.    
  6.     var l = 0
  7.     var r = s.count-1
  8.    
  9.     while l < r {
  10.        
  11.         // Пока не буква И не цифра двигаем указатель
  12.         while (!s[l].isLetter && !s[l].isNumber) && l < r {
  13.             l += 1
  14.         }
  15.        
  16.         while (!s[r].isLetter && !s[r].isNumber) && l < r {
  17.             r -= 1
  18.         }
  19.        
  20.         if s[l].lowercased() != s[r].lowercased() {
  21.             return false
  22.         }
  23.         l += 1
  24.         r -= 1
  25.     }
  26.     return true
  27. }
  28.  
  29. isPalindrome("A man, a plan, a canal: Panama")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement