leetcode_828_统计子串中的唯一字符

题意:所有子串中单个字符出现的次数和

问题转化:对于串中的每个字符,只包含其一次的所有子串的个数和

关于求只包含某位置字符一次的子串个数

cpp 复制代码
class Solution {
public:
    int uniqueLetterString(string s) {
        /* 
        ...A...A...A...
        */
       int n = s.size();
       vector<int> alpha(26, -1);
       vector<int> leftBound(n, -1); // 记录每个位置的左端点
       for(int i = 0; i < n; i++)
       {
           leftBound[i] = alpha[s[i] - 'A'];
           alpha[s[i] - 'A'] = i;
       }

       std::fill(alpha.begin(), alpha.end(), n);
       vector<int> rightBound(n, n); // 记录每个位置的右端点
       for(int i = n - 1; i >= 0; i--)
       {
           rightBound[i] = alpha[s[i] - 'A'];
           alpha[s[i] - 'A'] = i;
       }

       int ret = 0;
       for(int i = 0; i < n; i++)
       {
           ret += (i - leftBound[i]) * (rightBound[i] - i);
       }
       return ret;
        
    }
};
相关推荐
andafaAPS13 小时前
安达发|aps自动排产排程排单软件:日化生产高效运转“数字魔法”
大数据·人工智能·算法·aps软件·安达发aps·aps自动排产排程排单软件
黎阳之光13 小时前
全域实景立体管控:数字孪生与视频孪生技术体系白皮书
大数据·人工智能·算法·安全·数字孪生
88号技师14 小时前
2026年4月一区SCI-狒狒优化算法Baboon optimization algorithm-附Matlab免费代码
开发语言·算法·数学建模·matlab·优化算法
凯瑟琳.奥古斯特14 小时前
BFS解力扣1654最短跳跃次数
数据结构·算法·广度优先
sg_knight14 小时前
第一次用 OpenClaw,我让它 3 分钟写了个小工具
算法·llm·agent·ai编程·openclaw
m0_6294947314 小时前
LeetCode 热题 100-----23.反转链表
数据结构·算法·leetcode·链表
炸膛坦客14 小时前
嵌入式 - 数据结构与算法:(1-10)排序算法 - 冒泡排序(Bubble Sort)
算法·排序算法
Hello eveybody14 小时前
介绍一下动态树LCT(Python)
开发语言·python·算法
不穿铠甲的穿山甲14 小时前
MMR最大边际相关性
算法
handler0114 小时前
速通蓝桥杯省一:二分算法
c语言·开发语言·c++·笔记·算法·职场和发展·蓝桥杯