Suppose we have a string s with "x", "y" and "z"s, we have to find the number of subsequences that have i number of "x" characters, followed by j number of "y" characters and followed by k number of "z" characters where i, j, k ≥ 1.
So, if the input is like s = "xxyz", then the output will be 3, as we can make two "xyz" and one "xxyz"
To solve this, we will follow these steps:
n := size of s
x := 0, y := 0, z := 0
for i in range 0 to n, do
count := 0
if s[i] is same as "x", then
x := x * 2
x := x + 1
if s[i] is same as "y", then
y := y * 2
y := y + x
if s[i] is same as "z", then
z := z * 2
z := z + y
return z
Example
class Solution: def solve(self, s): n = len(s) x = 0 y = 0 z = 0 for i in range(n): count = 0 if s[i] == "x": x *= 2 x += 1 if s[i] == "y": y *= 2 y += x if s[i] == "z": z *= 2 z += y return z ob = Solution() print(ob.solve("xxyz"))
Input
"xxyz"
Output
3