22. Distinct occurrences
The problem can be found at the following link: Question Link
My Approach
This is a question of the take-non take type of dynamic programming, which involves breaking a string into multiple strings based on a specific condition. So I also used dynamic programming to solve this problem.
I define a recursive function
solve
to compute the number of distinct occurrences of stringt
in strings
starting from indicesi
andj
.At each step, we have two choices:
Not take the current character of
s
.If the current character of
s
matches the current character oft
, then we advance both pointers.
We memoize the results to avoid redundant computations.
Time and Auxiliary Space Complexity
Time Complexity:
(O(n x m))
, where(n)
is the length of strings
and(m)
is the length of stringt
.Auxiliary Space Complexity:
(O(n x m))
due to the memoization table.
Code (C++)
Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.
Last updated