Skip to content

Commit cc5655a

Browse files
authored
Create substrings-that-begin-and-end-with-the-same-letter.py
1 parent 9ca6582 commit cc5655a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
import collections
5+
6+
7+
class Solution(object):
8+
def numberOfSubstrings(self, s):
9+
"""
10+
:type s: str
11+
:rtype: int
12+
"""
13+
result = 0
14+
cnt = collections.Counter()
15+
for c in s:
16+
cnt[c] += 1
17+
result += cnt[c]
18+
return result
19+
20+
21+
# Time: O(n)
22+
# Space: O(1)
23+
import collections
24+
25+
26+
class Solution(object):
27+
def numberOfSubstrings(self, s):
28+
"""
29+
:type s: str
30+
:rtype: int
31+
"""
32+
return sum(v*(v+1)//2 for v in collections.Counter(s).itervalues())

0 commit comments

Comments
 (0)