leetcode 1156. 单字符重复子串的最大长度(C++)

如果字符串中的所有字符都相同,那么这个字符串是单字符重复的字符串。

给你一个字符串 text,你只能交换其中两个字符一次或者什么都不做,然后得到一些单字符重复的子串。返回其中最长的子串的长度。

 

示例 1:

输入:text = "ababa"
输出:3

示例 2:

输入:text = "aaabaaa"
输出:6

示例 3:

输入:text = "aaabbaaa"
输出:4

示例 4:

输入:text = "aaaaa"
输出:5

示例 5:

输入:text = "abcdef"
输出:1

 

提示:

  • 1 <= text.length <= 20000
  • text 仅由小写英文字母组成。

C++

class Solution {
public:
    int maxRepOpt1(string text) 
    {
        int n=text.length();
        int res=1;
        map<char,int> tmp;
        for(int i=0;i<n;i++)
        {
            tmp[text[i]]++;
        }
        if(1==n)
        {
            return 1;
        }
        else if(2==n)
        {
            if(text[0]==text[1])
            {
                return 2;
            }
            else
            {
                return 1;
            }
        }
        else
        {
            for(int i=1;i<n-1;i++)
            {
                int left=0;
                int right=0;
                int s=i-1;
                int t=i+1;
                while(s>=0 && text[s]==text[i-1])
                {
                    left++;
                    s--;
                }
                while(t<n && text[t]==text[i+1])
                {
                    right++;
                    t++;
                }
                if(text[i-1]==text[i+1])
                {
                    if(text[i-1]==text[i])
                    {
                        int val=left+right+1;
                        if(val<tmp[text[i]])
                        {
                            val++;
                        }
                        res=max(res,val);
                    }
                    else
                    {
                        if(left+right<tmp[text[i-1]])
                        {
                            res=max(res,left+right+1);
                        }
                        else
                        {
                            res=max(res,left+right);
                        }
                    }
                }
                else
                {
                    if(text[i-1]==text[i])
                    {
                        left++;
                    }
                    if(text[i+1]==text[i])
                    {
                        right++;
                    }
                    if(left<tmp[text[i-1]])
                    {
                        left++;
                    }
                    if(right<tmp[text[i+1]])
                    {
                        right++;
                    }
                    res=max(res,max(left,right));
                }
            }
            return res;
        }
    }
};

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值