首页 > 其他分享 >字符串中连续最多的字符&次数

字符串中连续最多的字符&次数

时间:2023-01-27 13:44:33浏览次数:34  
标签:字符 const str curValue res 次数 length charLen 字符串

  双层for循环

const repeatStrAndNumsByDoubleLoop = (str = 'aabbbcccddeeefffff') => {
    const res = {
        char: '',
        len: 0
    }
    const { length } = str
    for(let i = 0; i < length; i++){
        let charLen = 1
        const curValue = str[i]
        for(let j = i + 1; j < length; j++){
            const nextValue = str[j]
            if(curValue === nextValue){
                charLen += 1
            }
            if(curValue !== nextValue || j === length - 1){
                if(charLen > res.len){
                    res.char = curValue
                    res.len = charLen
                }
                i = j - 1 // 进行跳步 减少时间复杂度
                break
            }
        }
    }
    return res
}

  双指针  

  正则

  

标签:字符,const,str,curValue,res,次数,length,charLen,字符串
From: https://www.cnblogs.com/zhenjianyu/p/17068483.html

相关文章

  • OLED显示大字符和汉字及反显操作+51普中单片机
    1实验现象2实验原理(略)3系统设计(略)4硬件设计(略)5软件设计5.1主函数#include"OLED.H"#include"bmp.h"intmain(void){u8t='';//......
  • 力扣每一一题2023.1.26---1663. 具有给定数值的最小字符串
    小写字符的数值是它在字母表中的位置(从1开始),因此a的数值为1,b的数值为2,c的数值为3,以此类推。字符串由若干小写字符组成,字符串的数值为各字符的数值之和。例......
  • 力扣 871. 最低加油次数 [堆]
    871.最低加油次数汽车从起点出发驶向目的地,该目的地位于出发位置东面 target 英里处。沿途有加油站,每个 station[i] 代表一个加油站,它位于出发位置东面 stati......
  • C#利用String类的IndexOf、LastIndexOf、Substring截取字符串
    一、String.IndexOfString.IndexOf方法(Char,Int32,Int32)报告指定字符在此实例中的第一个匹配项的索引(从0开始)。搜索从指定字符位置开始,并检查指定数量的字符位置。S......
  • 求字符串最长重复子串的可视化演示
    求字符串最长重复子串的可视化演示假设字符串采用顺序存储结构,设计可视化算法将s中出现的第一个最长重复子串标记为红色。数据结构课程设计实验指导书数据结构教学团队......
  • KMP字符串匹配问题
    KMP算法本文参考资料:https://www.zhihu.com/question/21923021KMP算法是一种字符串匹配算法,可以在\(O(n+m)\)的时间复杂度内实现两个字符串的匹配。字符串匹配问题首......
  • C语言:判断回文字符串
    #include<stdio.h>#include<string.h>intfh(charab[]){intlen=strlen(ab),a=0;for(a=0;a<=len;a++)if(ab[a]!=ab[len-a-1])return0;retur......
  • C语言:数字字符串转数字求和
     #include<stdio.h>#include<string.h>main(){charzf[7],zfa[7];inta=0,b=0,c=0,len1,len2;gets(zf);gets(zfa);len1=strlen(zf),len2......
  • C语言字符串首字母大写
    #include<stdio.h>#include<string.h>main(){charch[100];inti=0,n=0;gets(ch);while(ch[i]!='\0'){if(i==0){......
  • C语言:scanf()输入多个字符串
    #include<stdio.h>#include<string.h>#include<stdlib.h>//利用<string.h>中的strtok函数,缺点就是异常复杂,但是优点就是可以用各种字符来分割输入的字符串intmain()......