문제
Longest Palindrome - LeetCode
Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome here. Example 1: Input: s = "abccccdd"Output: 7Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
![](https://leetcode.com/favicon-192x192.png)
![](https://leetcode.com/static/images/LeetCode_Sharing.png)
- 만들어질 수 있는 가장 긴 대칭 문자열의 길이 구하기
구현
- 모든 문자를 배열에 저장해서 카운트(boolean)
- 이미 저장되어 있다면 0(false)으로 초기화하며 길이를 2 증가
- 구한 길이가 처음 문자열의 길이보다 작다면 길이를 1 증가
시간 복잡도
- O(N)
코드
func longestPalindrome(s string) int { var length int cntArray := make([]bool,128,128) for _, ch := range s { if cntArray[ch] { cntArray[ch] = false length += 2 continue } cntArray[ch] = true } if length < len(s) { length++ } return length; }
'알고리즘' 카테고리의 다른 글
[LeetCode] 516. Longest Palindromic Subsequence (golang) (0) | 2021.06.14 |
---|---|
[LeetCode] 104. Maximum Depth of Binary Tree (golang) (0) | 2021.06.07 |
[LeetCode] 66. Plus One (golang) (0) | 2021.06.02 |
[LeetCode] 205. Isomorphic Strings (golang) (0) | 2021.06.02 |
[LeetCode] 572. Subtree of Another Tree (golang) (0) | 2021.06.01 |
댓글