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;
        
    }
};
相关推荐
神仙别闹1 天前
基于C++生成树思想的迷宫生成算法
开发语言·c++·算法
CoovallyAIHub1 天前
南京理工大学联手百度、商汤科技等团队推出Artemis:用结构化视觉推理革新多模态感知
深度学习·算法·计算机视觉
扣丁梦想家1 天前
面试基础整理之 ArrayList
面试·职场和发展
天才少女爱迪生1 天前
图像序列预测有什么算法方案
人工智能·python·深度学习·算法
GSDjisidi1 天前
日本IT行业|一些it資格证书分享解析,一篇通读
开发语言·面试·职场和发展
cici158741 天前
3D有限元直流电阻率法正演程序
算法·3d
陈陈爱java1 天前
综合素质面试hr面
面试·职场和发展
黑色的山岗在沉睡1 天前
滤波算法数学前置——线性化
线性代数·算法
t198751281 天前
火电机组热经济性分析MATLAB程序实现
人工智能·算法·matlab
Hello娃的1 天前
【半导体】肖特基接触AND欧姆接触
人工智能·算法