Created
June 27, 2018 12:43
-
-
Save digoreis/dd1c226c91c87d143f34c45a58507841 to your computer and use it in GitHub Desktop.
Function check if it is a permutation of a palindrome.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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