Fetch the repository succeeded.
/**
* 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 {
var i = 0, j = s.count - 1
let sChars = Array(s.lowercased())
while i < j {
while !sChars[i].isAlphanumeric && i < j {
i += 1
}
while !sChars[j].isAlphanumeric && i < j {
j -= 1
}
if sChars[i] != sChars[j] {
return false
} else {
i += 1
j -= 1
}
}
return true
}
}
extension Character {
var isValid: Bool {
return isLetter || isNumber
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。