Skip to content

Commit

Permalink
removing duplicate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
flohei committed Oct 18, 2018
1 parent 5a200e0 commit 73b0fc9
Showing 1 changed file with 0 additions and 42 deletions.
42 changes: 0 additions & 42 deletions Palindromes/Palindromes.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -1,49 +1,7 @@
//: Playground - noun: a place where people can play

// last checked with Xcode 9.0b4
#if swift(>=4.0)
print("Hello, Swift 4!")
#endif

import Foundation

/**
Validate that a string is a plaindrome
- parameter str: The string to validate
- returns: `true` if string is plaindrome, `false` if string is not
*/
func isPalindrome(_ str: String) -> Bool {
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
}

/**
Compares a strings left side character against right side character following
- parameter str: The string to compare characters of
- parameter left: Index of left side to compare, must be less than or equal to right
- parameter right: Index of right side to compare, must be greater than or equal to left
- returns: `true` if left side and right side have all been compared and they all match, `false` if a left and right aren't equal
*/
private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
if left >= right {
return true
}

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

if lhs != rhs {
return false
}

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

//true
isPalindrome("A man, a plan, a canal, Panama!")
isPalindrome("abbcbba")
Expand Down

0 comments on commit 73b0fc9

Please sign in to comment.