首页 > 其他分享 >Leetcode 1156. 单字符重复子串的最大长度

Leetcode 1156. 单字符重复子串的最大长度

时间:2023-06-04 15:55:47浏览次数:38  
标签:子串 字符 cnt idx int text 1156 maxlen Leetcode

题目:

如果字符串中的所有字符都相同,那么这个字符串是单字符重复的字符串。
给你一个字符串 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 仅由小写英文字母组成。

代码实现:

class Solution {
public:
    int maxRepOpt1(string text) {
        int n = text.size();
        unordered_map<char, int> hash; // 统计字符数量
        for(auto c : text) ++hash[c];

        int maxlen = 0; // 存结果
        int cnt[n], idx = -1;    // 将 aaabbaa 处理为 (a, 3) (b, 2) (a, 2)的形式

        for(int i = 0; i < n; ++i){
            // 统计连续字符
            int j = i;
            while(j < n && text[j] == text[i]) ++j;     // 统计到连续字符的下一个

            cnt[++idx] = j - i;    // 统计以text[i]开始的连续字符长度;

            // 判断存在两个相同子串 且 中间间隔一个不同字符
            if(idx > 1 && cnt[idx - 1] == 1 && i > 1 && text[i - 2] == text[i]){
                maxlen = max(maxlen, min(cnt[idx] + cnt[idx - 2] + 1, hash[text[i]]));
            }
            maxlen = max(maxlen, min(cnt[idx] + 1, hash[text[i]]));     // 其他情况时,小于总数则+1;
            i = j - 1;  // 直接扫描下一个不同字符
        }
        return maxlen;
    }
};

标签:子串,字符,cnt,idx,int,text,1156,maxlen,Leetcode
From: https://www.cnblogs.com/DL1024/p/17455788.html

相关文章

  • LeetCode 450. 删除二叉搜索树中的节点
    classSolution{public:TreeNode*deleteNode(TreeNode*root,intkey){del(root,key);returnroot;}voiddel(TreeNode*&root,intkey){if(!root)return;if(key<root->val)del(root->left......
  • #yyds干货盘点# LeetCode程序员面试金典:颠倒二进制位
    1.简述:颠倒给定的32位无符号整数的二进制位。提示:请注意,在某些语言(如Java)中,没有无符号整数类型。在这种情况下,输入和输出都将被指定为有符号整数类型,并且不应影响您的实现,因为无论整数是有符号的还是无符号的,其内部的二进制表示形式都是相同的。在Java中,编译器使用二进制补码......
  • LeetCode 501. 二叉搜索树中的众数
    classSolution{public:vector<int>res;intcnt=0;intfind(TreeNode*root,intval)//返回当前子树值为val的个数{if(!root)return0;returnfind(root->left,val)+find(root->right,val)+(val==root->val);}map&......
  • LeetCode.螺旋矩阵问题
    LeetCode54螺旋矩阵思路就是说,给我们一个二维数组,然后我们需要按顺时针的顺序遍历二维数组,然后把每一个遍历到的数据放到一个一维数组中,最后返回这个一维数组。思路很简单,关键是怎么控制让他顺时针去访问,什么时候向下走什么时候向左走,什么时候向右走等问题如图分析:但是......
  • leetcode2352哈希表的键可以是一个容器等类型
    map<vector<int>,int>cnt;//用于存储每个行向量出现的次数for(autorow:grid){//直接遍历行向量cnt[row]++;}for(inti=0;i<n;++i){vector<int>arr;for(intj=0;j<n;++j){//存储列向量arr.emplace_back(grid[j][i]);}if(cnt.find(arr)!=cnt.......
  • leetcode2352二维vector的操作
    对于二维vector有分外层和内层:当初始化指定了外层大小(行数)时,添加元素写法:错误写法:不能使用[]vector<vector<int>>v(3);//指定外层数目for(inti=0;i<3;++i){for(intj=0;j<n;++j){v[i][j]=0;}}正确写法:vector<vector<int>>v(3);//指定外层数目......
  • Leetcode 2559. 统计范围内的元音字符串数
    题目:给你一个下标从0开始的字符串数组words以及一个二维整数数组queries。每个查询queries[i]=[l,r]会要求我们统计在words中下标在l到r范围内(包含这两个值)并且以元音开头和结尾的字符串的数目。返回一个整数数组,其中数组的第i个元素对应第i个查询的答案......
  • LeetCode 236_ 二叉树的最近公共祖先
    classSolution{public:vector<TreeNode*>path1,path2;booldfs(TreeNode*root,TreeNode*p,vector<TreeNode*>&path){if(!root)returnfalse;if(root==p||dfs(root->left,p,path)||dfs(root->right,p,path))......
  • LeetCode235. 二叉搜索树的最近公共祖先
    classSolution{public:TreeNode*lowestCommonAncestor(TreeNode*root,TreeNode*p,TreeNode*q){if(p->val<root->val&&q->val<root->val)returnlowestCommonAncestor(root->left,p,q);if(p->v......
  • leetcode 1341 电影评分
    leetcode1341 电影评分(selectu1.nameasresultsfromUsersu1leftjoin(selectmr1.user_id,count(mr1.rating)asc1fromMovieRatingasmr1groupbymr1.user_idhavingc1=(selectmax(p.c2)fromUsersasu2......