Skip to content

Instantly share code, notes, and snippets.

@bhakes
Created November 13, 2018 16:45
Show Gist options
  • Save bhakes/9342175a04f23f16367c0a43bcdd559f to your computer and use it in GitHub Desktop.
Save bhakes/9342175a04f23f16367c0a43bcdd559f to your computer and use it in GitHub Desktop.
// I did this the hard way and it would have been a lot
// easier to do it using this Hint:
// Hint: In order for a word to be a palindrome, is needs to have an even amount of the same letters on each side with the exception of a single letter in the middle
// pseudo code of easier way:
// for each letter in the string
// save it to a key pair array
// key being letter
// pair being count
// if all key pairs have even values
// then it can be a palindrome
// if all but one are even then it can be a palindrome
// if two are odd then not
import UIKit
import Foundation
var str = "Hello, playground"
func canBePalindrome(_ str: String){
//
// get string into set of strings
var subString : Character
var subStringArray : [String] = []
var index : Int = 0
for index in 0..<str.count {
let temp = str.index (str.startIndex, offsetBy: index)
subString = str[temp]
subStringArray.append(String(subString))
}
print(subStringArray)
// build the possible strings
// call palindromeChecker(_ strArray : [String]) on possible strings
// if return return false, continue, until then end and return false/
// if return true, early return true.
}
// for char in str
canBePalindrome("carrace")
// palindrome checker
func palindromeChecker(_ strArray : [String])->String{
var tail: Int = 0
var head: Int = strArray.count - 1
while (head > tail) {
if(strArray[head] == strArray[tail]){
head -= 1
tail += 1
continue
}
else { return "false"}
}
return "true"
}
func permuteWirth<T>(_ a: [T], _ n: Int) {
if n == 0 {
print(a) // display the current permutation
} else {
var a = a
permuteWirth(a, n - 1)
for i in 0..<n {
a.swapAt(i, n)
permuteWirth(a, n - 1)
a.swapAt(i, n)
}
}
}
var testPalindrome: [String] = ["a","b","b"]
let result = palindromeChecker(testPalindrome)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment