Tuesday, 28 March 2023

Count the Substrings [29/03/2023]

Count the Substrings.py

Python Code !! 

#{ 

 # Driver Code Starts

#Initial Template for Python 3

from collections import Counter


class Solution:

    def countSubstring(self, S): 

        #code here

        n = len(S)

        d = Counter()

        d[0] = 1

        cnt = 0

        ans = 0

        for c in S:

            if c.isupper():

                cnt += 1

            else:

                cnt -= 1

            ans += d[cnt];

            d[cnt] += 1

        return ans


#{ 

 # Driver Code Starts.

if __name__ == '__main__': 

    t = int(input())

    for _ in range(t):

        S = input()

        ob = Solution()

        ans = ob.countSubstring(S)

        print(ans)


# } Driver Code Ends

No comments:

Post a Comment

GeeksforGeeks and LeetCode - Problem Of The Day

Welcome to our Problem of the Day Solutions page, where we provide comprehensive solutions to daily programming challenges from two renowned...