当前位置:网站首页>LeetCode_ 409_ Longest palindrome string
LeetCode_ 409_ Longest palindrome string
2022-07-28 03:42:00 【Fitz1318】
Topic link
Title Description
Given a string of uppercase and lowercase letters s , return Constructed from these letters The longest palindrome string .
In the process of construction , Please note that Case sensitive . such as "Aa" Can't be treated as a palindrome string .
Example 1:
Input :s = "abccccdd"
Output :7
explain :
The longest palindrome string we can construct is "dccaccd", Its length is 7.
Example 2:
Input :s = "a"
Input :1
Example 3:
Input :s = "bb"
Input : 2
Tips :
1 <= s.length <= 2000
s Only lowercase and / Or capital letters
Their thinking
- Traversal string , And use HashSet To record , If it doesn't appear, save it in set in , If it happens , Then turn the letters that have appeared from set Delete in , And put the answer +2
- Last set There are odd numbers of letters stored in , Judge set Whether the set is empty , If it's not empty , Just put the answer +1
AC Code
class Solution {
public int longestPalindrome(String s) {
int ans = 0;
HashSet<Character> set = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (set.contains(c)) {
set.remove(c);
ans += 2;
} else {
set.add(c);
}
}
if (set.size() > 0) {
ans += 1;
}
return ans;
}
}
边栏推荐
- 高等数学(第七版)同济大学 习题3-6 个人解答
- 过亿资产地址被拉入黑名单?Tether地址冻结功能该怎么用?
- D2dengine edible tutorial (4) -- draw text
- Interface automation test, complete introduction
- ES6 从入门到精通 # 07:解构赋值
- Golang gets the tag of the loop nested structure
- ES6 from getting started to mastering 08: extended object functions
- MangoPapa 的实用小脚本(目录篇)
- Weekly recommended short video: how to correctly understand the word "lean"?
- [openvx] VX for basic use of objects_ matrix
猜你喜欢

verticle-align行内元素垂直居中对齐

BRD,MRD,PRD的区别

【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置

8000 word explanation of OBSA principle and application practice

ES6 从入门到精通 # 09:Symbol 类型

Collection | 0 basic open source data visualization platform flyfish large screen development guide

Weekly recommended short video: how to correctly understand the word "lean"?

一篇文章掌握Postgresql中对于日期类数据的计算和处理

Qt:qmessagebox message box, custom signal and slot

构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!
随机推荐
695. Maximum area of the island
Qt:qmessagebox message box, custom signal and slot
一篇文章掌握Postgresql中对于日期类数据的计算和处理
递归和非递归分别实现求第n个斐波那契数
数据挖掘-02
Unity背包系统
ES6 from entry to mastery 07: Deconstruction assignment
2022-07-27: Xiao Hong got an array arr with a length of N. she is going to modify it only once. She can modify any number arr[i] in the array to a positive number not greater than P (the modified numb
[wrong question]mocha and railgun
Interview essential skills: SQL query special training!
The open source of "avoiding disease and avoiding medicine" will not go far
[openvx] VX for basic use of objects_ lut
[错题]Concatenation
Greedy - 53. Maximum subarray sum
ES6 from getting started to mastering 08: extended object functions
动态规划——63. 不同路径 II
695. 岛屿的最大面积
Outlook 教程,如何在 Outlook 中使用颜色类别和提醒?
LabVIEW加载和使用树型控件项目中的定制符号
【OPENVX】对象基本使用之vx_image