Skip to content

Commit

Permalink
Update README formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lostatseajoshua committed Dec 22, 2016
1 parent 3304848 commit 0ffeb1f
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions Palindromes/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,49 @@ Algorithms that check for palindromes are a common programming interview questio
## Example

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`.

`raceCar`
`r a c e c a r`
`r?a?c?e?c?a?r?`
`RACEcar`

## Algorithm

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.

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.
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.

## The code

Here is a recursive implementation of this in Swift:

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

if length > 1 {
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
}
return false
if length > 1 {
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
}
return false
}

private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
if left >= right {
return true
}
if left >= right {
return true
}

let lhs = str[str.index(str.startIndex, offsetBy: left)]
let rhs = str[str.index(str.startIndex, offsetBy: right)]
let lhs = str[str.index(str.startIndex, offsetBy: left)]
let rhs = str[str.index(str.startIndex, offsetBy: right)]

if lhs != rhs {
return false
}
if lhs != rhs {
return false
}

return palindrome(str, left: left + 1, right: right - 1)
return palindrome(str, left: left + 1, right: right - 1)
}
```

This algorithm has a two-step process.
This algorithm has a two-step process.

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.

```swift
Expand All @@ -61,7 +61,7 @@ The length of the string is then checked to make sure that the string after bein
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.
```swift
if left >= right {
return true
return true
}
```
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.
Expand All @@ -70,7 +70,7 @@ let lhs = str[str.index(str.startIndex, offsetBy: left)]
let rhs = str[str.index(str.startIndex, offsetBy: right)]

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

Step 2:
```
| |
racecar -> r == r
| |
racecar -> a == a
| |
racecar -> c == c
|
racecar -> left index == right index -> return true
racecar -> left index == right index -> return true```

## Additional Resources

Expand Down

0 comments on commit 0ffeb1f

Please sign in to comment.