用自己网站做邮箱域名,网站建设开发收费,男男床做第一次视频网站,厦门 网站建设企业邮箱统计子串中的唯一字符
力扣链接#xff1a;828. 统计子串中的唯一字符
题目描述
我们定义了一个函数 countUniqueChars(s) 来统计字符串 s 中的唯一字符#xff0c;并返回唯一字符的个数。
例如#xff1a;s “LEETCODE” #xff0c;则其中 “L”, “T”,“C”,“O”…统计子串中的唯一字符
力扣链接828. 统计子串中的唯一字符
题目描述
我们定义了一个函数 countUniqueChars(s) 来统计字符串 s 中的唯一字符并返回唯一字符的个数。
例如s “LEETCODE” 则其中 “L”, “T”,“C”,“O”,“D” 都是唯一字符因为它们只出现一次所以 countUniqueChars(s) 5 。
本题将会给你一个字符串 s 我们需要返回 countUniqueChars(t) 的总和其中 t 是 s 的子字符串。输入用例保证返回值为 32 位整数。
注意某些子字符串可能是重复的但你统计时也必须算上这些重复的子字符串也就是说你必须统计 s 的所有子字符串中的唯一字符。
示例
示例 1
输入: s “ABC” 输出: 10 解释: 所有可能的子串为“A”,“B”,“C”,“AB”,“BC” 和 “ABC”。 其中每一个子串都由独特字符构成。 所以其长度总和为1 1 1 2 2 3 10 示例 2
输入: s “ABA” 输出: 8 解释: 除了 countUniqueChars(“ABA”) 1 之外其余与示例 1 相同。 示例 3
输入s “LEETCODE” 输出92
代码
class Solution {public int uniqueLetterString(String s) {MapCharacter, ListInteger index new HashMapCharacter, ListInteger();for (int i 0; i s.length(); i) {char c s.charAt(i);if (!index.containsKey(c)) {index.put(c, new ArrayListInteger());index.get(c).add(-1);}index.get(c).add(i);}int res 0;for (Map.EntryCharacter, ListInteger entry : index.entrySet()) {ListInteger arr entry.getValue();arr.add(s.length());for (int i 1; i arr.size() - 1; i) {res (arr.get(i) - arr.get(i - 1)) * (arr.get(i 1) - arr.get(i));}}return res;}
}