forked from NilaakashSingh/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidPalindrome.swift
28 lines (25 loc) · 896 Bytes
/
ValidPalindrome.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Question Link: https://leetcode.com/problems/valid-palindrome/
* Primary idea: For every index in the first half of the String, compare two values at mirroring indices.
*
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class ValidPalindrome {
func isPalindrome(_ s: String) -> Bool {
// Make String into an array of lowercase Characters
let characters = s.lowercased().characters
// Only keep alphanumeric characters.
let cleaned = characters.filter { character in
return character.description.rangeOfCharacter(from: CharacterSet.alphanumerics) != nil
}
// Compare values at mirroring indices.
let total = cleaned.count
for i in 0 ..< total/2 {
if cleaned[i] != cleaned[total - 1 - i] {
return false
}
}
return true
}
}