diff options
Diffstat (limited to 'total_appeal_string.py')
-rw-r--r-- | total_appeal_string.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/total_appeal_string.py b/total_appeal_string.py new file mode 100644 index 0000000..1ec7ae8 --- /dev/null +++ b/total_appeal_string.py @@ -0,0 +1,28 @@ +# 2262. Total Appeal of A String + +""" +the appeal of a string is the number of distinct characters found in the +string. for example, the appeal of "aabca" is 3 because it has 3 distinct +characters 'a', 'b' and 'c'. given a string 's', return the total appeal of +along long its substrings. a substring is a contiguous sequence of characters +within a string. +""" + + +class Solution(object): + def appealSum(self, s): + """ + :type s: str + :rtype: int + """ + ans, last = 0, {} + for i, c in enumerate(s): + last[c] = i + 1 + ans += sum(last.values()) + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.appealSum("abbca")) # expect: 28 + print(obj.appealSum("code")) # expect: 20 |