Uber Interview - Senior Software Engineer - Nov 2024
You are given a string s, and you have to count the total number of unique substring, where the substring have the same number of frequency of each unique character in that substring. Also the given string can have only digits from '0' to '9'.
Ex 1:
given stirng s = "1212"
answer is 5 : "1", "2", "12", "21" & "1212".
As "12" have each unique char 1 and 2 have frequency 1 and similarly in "1212" we have unique chars 1 and 2 with each having frequency of 2
Here "121" is not valid substring as the frequency of 1 and 2 are not equal.
Ex 2:
s = "11202"
answer is 8 : "1", "2", "0", "11", "12", "20", "02", "120"
here "2" and "02" will be considered as different string.
I could able to write code using sliding window with O(N^2) time and O(N^3) space complesity. Interviewer insisting on reducing the space complexity to a lower order.
To figureout the unique string we need to store out max n^2 string and each can be of lenght n. so the complexity was O(n^3).
Any suggestion to reduce the space complexity and even time if possible.


