-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
125 Valid Palindrome.swift
48 lines (39 loc) 路 1.12 KB
/
125 Valid Palindrome.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// 125 Valid Palindrome.swift
// LeetCode-Solutions
//
// Created by Aleksandar Dinic on 03/08/2020.
// Copyright 漏 2020 Aleksandar Dinic. All rights reserved.
//
import Foundation
/// Source: https://leetcode.com/problems/valid-palindrome/
class Solution {
/// Determines if a string is a palindrome.
///
/// - Parameter s: The string.
/// - Returns: True if the string is a palindrome, otherwise returns false.
///
/// - Complexity:
/// - time: O(n), where n is the length of the *s*.
/// - space: O(1), only constant space is used.
func isPalindrome(_ s: String) -> Bool {
guard !s.isEmpty else { return true }
let s = Array(s)
var i = 0
var j = s.count - 1
while i < j {
if !s[i].isLetter, !s[i].isNumber {
i += 1
continue
}
if !s[j].isLetter, !s[j].isNumber {
j -= 1
continue
}
guard s[i].lowercased() == s[j].lowercased() else { return false }
i += 1
j -= 1
}
return true
}
}