Skip to content

Commit 0ffeb1f

Browse files
Update README formatting
1 parent 3304848 commit 0ffeb1f

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

Palindromes/README.markdown

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,49 @@ Algorithms that check for palindromes are a common programming interview questio
77
## Example
88

99
The word racecar is a valid palindrome, as it is a word spelled the same when backgrounds and forwards. The examples below shows valid cases of the palindrome `racecar`.
10+
1011
`raceCar`
1112
`r a c e c a r`
1213
`r?a?c?e?c?a?r?`
1314
`RACEcar`
1415

1516
## Algorithm
1617

17-
To check for palindromes, a strings character are compared starting from the beginning and end and moving inward moving to toward the middle of the string and keep the same distance apart in the comparison index.
18-
19-
In this implementation of a palindrome algorithm, recursion is used to check each of the characters on the left-hand side and right-hand side moving inward.
18+
To check for palindromes, a string's characters are compared starting from the beginning and end then moving inward toward the middle of the string while maintaining the same distance apart. In this implementation of a palindrome algorithm, recursion is used to check each of the characters on the left-hand side and right-hand side moving inward.
2019

2120
## The code
2221

2322
Here is a recursive implementation of this in Swift:
2423

2524
```swift
2625
func isPalindrome(_ str: String) -> Bool {
27-
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
28-
let length = strippedString.characters.count
26+
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
27+
let length = strippedString.characters.count
2928

30-
if length > 1 {
31-
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
32-
}
33-
return false
29+
if length > 1 {
30+
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
31+
}
32+
return false
3433
}
3534

3635
private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
37-
if left >= right {
38-
return true
39-
}
36+
if left >= right {
37+
return true
38+
}
4039

41-
let lhs = str[str.index(str.startIndex, offsetBy: left)]
42-
let rhs = str[str.index(str.startIndex, offsetBy: right)]
40+
let lhs = str[str.index(str.startIndex, offsetBy: left)]
41+
let rhs = str[str.index(str.startIndex, offsetBy: right)]
4342

44-
if lhs != rhs {
45-
return false
46-
}
43+
if lhs != rhs {
44+
return false
45+
}
4746

48-
return palindrome(str, left: left + 1, right: right - 1)
47+
return palindrome(str, left: left + 1, right: right - 1)
4948
}
5049
```
5150

52-
This algorithm has a two-step process.
51+
This algorithm has a two-step process.
52+
5353
1. The first step is to pass the string to validate as a palindrome into the `isPalindrome` method. This method first removes occurrences of non-word pattern matches `\W` [Regex reference](http://regexr.com). It is written with two \\ to escape the \ in the String literal.
5454

5555
```swift
@@ -61,7 +61,7 @@ The length of the string is then checked to make sure that the string after bein
6161
2. The second step is to pass the string in a recursive method. This method takes a string, a left index, and a right index. The method checks the characters of the string using the indexes to compare each character on both sides. The method checks if the left is greater or equal to the right if so the entire string has been run through without returning false so the string is equal on both sides thus returning true.
6262
```swift
6363
if left >= right {
64-
return true
64+
return true
6565
}
6666
```
6767
If the check doesn't pass it continues to get the characters at the specified indexes and compare each. If they are not the same the method returns false and exits.
@@ -70,7 +70,7 @@ let lhs = str[str.index(str.startIndex, offsetBy: left)]
7070
let rhs = str[str.index(str.startIndex, offsetBy: right)]
7171

7272
if lhs != rhs {
73-
return false
73+
return false
7474
}
7575
```
7676
If they are the same the method calls itself again and updates the indexes accordingly to continue to check the rest of the string.
@@ -82,14 +82,18 @@ Step 1:
8282
` race?C ar -> raceCar -> racecar``
8383

8484
Step 2:
85+
```
8586
| |
8687
racecar -> r == r
88+
8789
| |
8890
racecar -> a == a
91+
8992
| |
9093
racecar -> c == c
94+
9195
|
92-
racecar -> left index == right index -> return true
96+
racecar -> left index == right index -> return true```
9397
9498
## Additional Resources
9599

0 commit comments

Comments
 (0)