Leetcode # 2083. Substrings That Begin and End With the Same Letter

Problem

https://leetcode.com/problems/substrings-that-begin-and-end-with-the-same-letter

Solution

Time Complexity: O(len(s))
Space Complexity: O(len(s))
(The input and output generally do not count towards the space complexity.)

class Solution:
  def numberOfSubstrings(self, s: str) -> int:
    ans = len(s)
    letter_seen = Counter()
    for c in s:
      if c in letter_seen: ans += letter_seen[c]
      letter_seen[c] += 1
    return ans

 

Last Updated on 2024/06/08 by A1go

目錄
Bitnami