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;
        
    }
};
相关推荐
xlp666hub16 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
有意义18 小时前
深度拆解分割等和子集:一维DP数组与倒序遍历的本质
前端·算法·面试
xlp666hub19 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
用户7268761033719 小时前
解放双手的健身助手:基于 Rokid AR 眼镜的运动计时应用
算法
Wect20 小时前
LeetCode 17. 电话号码的字母组合:回溯算法入门实战
前端·算法·typescript
xlp666hub2 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
ZhengEnCi2 天前
08c. 检索算法与策略-混合检索
后端·python·算法
程序员小崔日记2 天前
大三备战考研 + 找实习:我整理了 20 道必会的时间复杂度题(建议收藏)
算法·408·计算机考研
lizhongxuan2 天前
AI小镇 - 涌现
算法·架构
AI工程架构师2 天前
通常说算力是多少 FLOPS,怎么理解,GPU和CPU为什么差异这么大
算法