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
let lhs = str[str.index(str.startIndex, offsetBy: left)]
43
-
let rhs = str[str.index(str.startIndex, offsetBy: right)]
42
+
let lhs = str[str.index(str.startIndex, offsetBy: left)]
43
+
let rhs = str[str.index(str.startIndex, offsetBy: right)]
44
44
45
-
if lhs != rhs {
46
-
returnfalse
47
-
}
45
+
if lhs != rhs {
46
+
returnfalse
47
+
}
48
48
49
-
returnpalindrome(str, left: left +1, right: right -1)
49
+
returnpalindrome(str, left: left +1, right: right -1)
50
50
}
51
51
```
52
52
53
53
This algorithm has a two-step process.
54
54
55
-
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.
55
+
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.
The length of the string is then checked to make sure that the string after being stripped of non-word characters is still in a valid length. It is then passed into the next step after being lowercased.
62
62
63
-
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.
63
+
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.
64
64
```swift
65
65
if left >= right {
66
-
returntrue
66
+
returntrue
67
67
}
68
68
```
69
69
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.
@@ -72,7 +72,7 @@ let lhs = str[str.index(str.startIndex, offsetBy: left)]
72
72
let rhs = str[str.index(str.startIndex, offsetBy: right)]
73
73
74
74
if lhs != rhs {
75
-
returnfalse
75
+
returnfalse
76
76
}
77
77
```
78
78
If they are the same the method calls itself again and updates the indexes accordingly to continue to check the rest of the string.
@@ -81,20 +81,21 @@ return palindrome(str, left: left + 1, right: right - 1)
81
81
```
82
82
83
83
Step 1:
84
-
` race?C ar -> raceCar -> racecar``
84
+
85
+
`race?C ar -> raceCar -> racecar`
85
86
86
87
Step 2:
87
88
```
88
89
| |
89
90
racecar -> r == r
90
91
91
-
| |
92
+
| |
92
93
racecar -> a == a
93
94
94
-
| |
95
+
| |
95
96
racecar -> c == c
96
97
97
-
|
98
+
|
98
99
racecar -> left index == right index -> return true
0 commit comments