You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Palindromes/README.markdown
+26-22Lines changed: 26 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -7,49 +7,49 @@ Algorithms that check for palindromes are a common programming interview questio
7
7
## Example
8
8
9
9
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
+
10
11
`raceCar`
11
12
`r a c e c a r`
12
13
`r?a?c?e?c?a?r?`
13
14
`RACEcar`
14
15
15
16
## Algorithm
16
17
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.
20
19
21
20
## The code
22
21
23
22
Here is a recursive implementation of this in Swift:
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)]
43
42
44
-
if lhs != rhs {
45
-
returnfalse
46
-
}
43
+
if lhs != rhs {
44
+
returnfalse
45
+
}
47
46
48
-
returnpalindrome(str, left: left +1, right: right -1)
47
+
returnpalindrome(str, left: left +1, right: right -1)
49
48
}
50
49
```
51
50
52
-
This algorithm has a two-step process.
51
+
This algorithm has a two-step process.
52
+
53
53
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.
54
54
55
55
```swift
@@ -61,7 +61,7 @@ The length of the string is then checked to make sure that the string after bein
61
61
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.
62
62
```swift
63
63
if left >= right {
64
-
returntrue
64
+
returntrue
65
65
}
66
66
```
67
67
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)]
70
70
let rhs = str[str.index(str.startIndex, offsetBy: right)]
71
71
72
72
if lhs != rhs {
73
-
returnfalse
73
+
returnfalse
74
74
}
75
75
```
76
76
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:
82
82
` race?C ar -> raceCar -> racecar``
83
83
84
84
Step 2:
85
+
```
85
86
| |
86
87
racecar -> r == r
88
+
87
89
| |
88
90
racecar -> a == a
91
+
89
92
| |
90
93
racecar -> c == c
94
+
91
95
|
92
-
racecar -> left index == right index -> return true
96
+
racecar -> left index == right index -> return true```
0 commit comments