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;
        
    }
};
相关推荐
Jasmine_llq9 小时前
《CF280C Game on Tree》
数据结构·算法·邻接表·深度优先搜索(dfs)·树的遍历 + 线性累加统计
小棠师姐9 小时前
支持向量机(SVM)入门:超平面与核函数的通俗解释
算法·python机器学习·支持向量机svm·超平面可视化·核函数应用
im_AMBER10 小时前
Leetcode 102 反转链表
数据结构·c++·学习·算法·leetcode·链表
今儿敲了吗10 小时前
01|多项式输出
c++·笔记·算法
Xの哲學10 小时前
深入剖析Linux文件系统数据结构实现机制
linux·运维·网络·数据结构·算法
AlenTech11 小时前
200. 岛屿数量 - 力扣(LeetCode)
算法·leetcode·职场和发展
C雨后彩虹11 小时前
竖直四子棋
java·数据结构·算法·华为·面试
不如自挂东南吱11 小时前
空间相关性 和 怎么捕捉空间相关性
人工智能·深度学习·算法·机器学习·时序数据库
洛生&12 小时前
Elevator Rides
算法
2501_9335130412 小时前
关于一种计数的讨论、ARC212C Solution
算法