Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let sentence = "madam anna kayak notapalindrome anna civic racecar"
- var dict:[String:Int] = [:]
- func isPalindrome(str:String) -> Bool{
- if (str.characters.count == 0 || str.characters.count == 1){
- return true
- }
- else if str.first != str.last{
- return false
- }
- else{
- var crop = str
- crop.removeLast()
- crop.removeFirst()
- return isPalindrome(str: crop)
- }
- }
- for s in sentence.split(separator: Character(" ")){
- let word:String = String(s.characters)
- if isPalindrome(str: word){
- if dict[word] == nil{
- dict[word] = 1
- continue
- }
- dict[word]! += 1
- }
- }
- print(dict)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement