Skip to content

Instantly share code, notes, and snippets.

@digoreis
Created June 27, 2018 12:43
Show Gist options
  • Save digoreis/dd1c226c91c87d143f34c45a58507841 to your computer and use it in GitHub Desktop.
Save digoreis/dd1c226c91c87d143f34c45a58507841 to your computer and use it in GitHub Desktop.
Function check if it is a permutation of a palindrome.
import Cocoa
struct CounterPalindrome {
var countOdd = 0
var isPermutationAndPalindrome: Bool {
return countOdd <= 1
}
var table = Dictionary<Character,Int>()
mutating func count(_ char: Character) {
table[char] = (table[char] ?? 0) + 1
if(((table[char] ?? 0) % 2) == 1) {
countOdd += 1
} else {
countOdd -= 1
}
}
}
func isPermutationAndPalindrome(str: String) -> Bool {
let prepareStr = str.lowercased().filter({$0 != Character(" ")})
var counter = CounterPalindrome()
prepareStr.forEach { c in
counter.count(c)
}
return counter.isPermutationAndPalindrome
}
isPermutationAndPalindrome(str: "Tact Coa")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment