最长回文串

描述:给出一个包含大小写字母的字符串。求出由这些字母构成的最长的回文串的长度是多少。数据是大小写敏感的,也就是说,”Aa” 并不会被认为是一个回文串。

class Solution {
public:
    /**
     * @param s a string which consists of lowercase or uppercase letters
     * @return the length of the longest palindromes that can be built
     */
    int longestPalindrome(string& s) {
        // Write your code here
        int len = s.length();
        if (len <= 0)
    {
        return 0;
    }

    vector<char> vector_char;
    unordered_set<char> unordset_char;
    const char* p = s.c_str();
    while (*p != '\0')
    {
        auto it = unordset_char.find(*p);
        if (it != unordset_char.end())
        {
            unordset_char.erase(it);
            vector_char.push_back(*p);
        }
        else
        {
            unordset_char.insert(*p);
        }
        ++p;
    }

    int size = vector_char.size() * 2;
    if (!unordset_char.empty())
    {
        ++size;
    }

    return size;
    }
};

样例:
给出 s = “abccccdd” 返回 7
一种可以构建出来的最长回文串方案是 “dccaccd”。

总耗时: 43 ms

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值