Shawn's Blog

  • Home
  • Tags
  • Categories
  • Archives
  • Search
  • Table of Contents
  • Overview

Shawn

若有恒,何必三更眠五更起;最无益,莫过一日曝十日寒。
392 posts
13 categories
114 tags

LeetCode 2262. Total Appeal of A String

Posted on 2022-05-02 Edited on 2024-11-24 In LeetCode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
long long appealSum(string s) {
int n = s.size();
long long ans = 0L, sum = 0L;
vector<int> pos(26, -1);
for (int i = 0; i < n; ++i) {
int c = s[i] - 'a';
sum += i - pos[c];
ans += sum;
pos[c] = i;
}
return ans;
}
};
# Dynamic Programming# String# Hash Table
LeetCode 2258. Escape the Spreading Fire
LeetCode 2261. K Divisible Elements Subarrays
© 2024 Shawn
Powered by Hexo & NexT.Mist
0%