Skip to content

Commit 756cc8e

Browse files
nktsskNikita Sosyuk
authored and
Nikita Sosyuk
committed
Add string palindrome algorithm
1 parent 2ce2905 commit 756cc8e

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// A palindrome is a string that reads the same forwards and backwards.
2+
//
3+
// Examples: "level", "radar", "madam", "A man, a plan, a canal: Panama".
4+
5+
extension String {
6+
7+
/// Iteratively comparing characters from the beginning and end of the string. Only include letters and numbers.
8+
/// - Complexity: O(n), without allocating new space.
9+
func isPalindrome() -> Bool {
10+
var leftIndex = startIndex
11+
var rightIndex = index(before: endIndex)
12+
13+
while leftIndex < rightIndex {
14+
guard self[leftIndex].isLetter || self[leftIndex].isNumber else {
15+
leftIndex = index(after: leftIndex)
16+
continue
17+
}
18+
guard self[rightIndex].isLetter || self[rightIndex].isNumber else {
19+
rightIndex = index(before: rightIndex)
20+
continue
21+
}
22+
guard self[leftIndex].lowercased() == self[rightIndex].lowercased() else {
23+
return false
24+
}
25+
26+
leftIndex = index(after: leftIndex)
27+
rightIndex = index(before: rightIndex)
28+
}
29+
30+
return true
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// A palindrome is a string that reads the same forwards and backwards.
2+
//
3+
// Examples: "level", "radar", "madam", "A man, a plan, a canal: Panama".
4+
5+
extension String {
6+
7+
/// Recursively comparing characters from the beginning and end of the string. Only include letters and numbers.
8+
/// - Complexity: O(n), without allocating new space.
9+
func isPalindrome() -> Bool {
10+
isPalindromeRecursion(
11+
leftIndex: startIndex,
12+
rightIndex: index(before: endIndex)
13+
)
14+
}
15+
16+
private func isPalindromeRecursion(
17+
leftIndex: String.Index,
18+
rightIndex: String.Index
19+
) -> Bool {
20+
guard leftIndex < rightIndex else {
21+
return true
22+
}
23+
guard self[leftIndex].isLetter || self[leftIndex].isNumber else {
24+
return isPalindromeRecursion(
25+
leftIndex: index(after: leftIndex),
26+
rightIndex: rightIndex
27+
)
28+
}
29+
guard self[rightIndex].isLetter || self[rightIndex].isNumber else {
30+
return isPalindromeRecursion(
31+
leftIndex: leftIndex,
32+
rightIndex: index(before: rightIndex)
33+
)
34+
}
35+
guard self[leftIndex].lowercased() == self[rightIndex].lowercased() else {
36+
return false
37+
}
38+
39+
return isPalindromeRecursion(
40+
leftIndex: index(after: leftIndex),
41+
rightIndex: index(before: rightIndex)
42+
)
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// A palindrome is a string that reads the same forwards and backwards.
2+
//
3+
// Examples: "level", "radar", "madam", "A man, a plan, a canal: Panama".
4+
5+
extension String {
6+
7+
/// Using the `reverse()` method to reverse the string and comparing it with the original. Only include letters and numbers.
8+
/// - Complexity: O(n), with allocating O(n) space.
9+
func isPalindrome() -> Bool {
10+
let input = lowercased().filter { $0.isLetter || $0.isNumber }
11+
return input == String(input.reversed())
12+
}
13+
}

0 commit comments

Comments
 (0)