Leetcode - Longest Palindromic Substring
Leetcode - Longest Palindrome

Leetcode - Palindrome Permutation

violet posted @ Mar 25, 2020 01:13:17 AM in 算法 with tags Algorithm Golang , 52 阅读

https://leetcode.com/problems/palindrome-permutation/

Given a string, determine if a permutation of the string could form a palindrome.

Example 1:

Input: "code"
Output: false

Example 2:

Input: "aab"
Output: true

Example 3:

Input: "carerac"
Output: true

 

func canPermutePalindrome(s string) bool {
    hash := map[byte]int{}
    for i := 0; i < len(s); i++ {
        hash[s[i]]++
    }
    
    oddCount := 0
    for _, val := range hash {
        if val % 2 == 1 {
            oddCount++
        }
    }
    if oddCount > 1{
        return false
    }
    return true
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter