|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 糖逗 于 2020-5-8 17:52 编辑
题目描述:
- 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串。
- 在构造过程中,请注意区分大小写。比如 "Aa" 不能当做一个回文字符串。
- 注意:
- 假设字符串的长度不会超过 1010。
- 示例 1:
- 输入:
- "abccccdd"
- 输出:
- 7
- 解释:
- 我们可以构造的最长的回文串是"dccaccd", 它的长度是 7。
复制代码
- int longestPalindrome(string s) {
- map<char, int> temp;
- int res = 0;
- int temp1 = 0;
- for(auto cha : s){
- temp[cha] ++;
- }
- for(auto cha : s){
- if(temp[cha] == 0) continue;
- if(temp[cha] % 2 == 0){
- res+=temp[cha];
- }
- else if(temp[cha] %2 == 1){
- res += temp[cha]-1;
- temp1 = 1;
- }
- temp[cha] = 0;
- }
- res += temp1;
- return res;
- }
复制代码
注意事项:
1.参考链接:https://leetcode-cn.com/problems ... ng-100ti-jie-by-yx/ |
|