![]() |
VOOZH | about |
Given a string str of length of N that is in the encoded form with alphabets and * . The task is to find the string from which it was generated. The required string can be generated from the encoded string by replacing all the * with the prefix values of the encoded string.
Examples:
Input: str = ab*c*d
Output: "ababcababcd"
Explanation: For the first occurrence of "*", "ab" is the prefix. So '*' is replaced by "ab" resulting the string to be "ababc*d" and for the next '*' the prefix is "ababc". So the string will now change from "ababc*d" to "ababcababcd".Input : str = "z*z*z"
Output: zzzzzzz
Approach: The solution is based on greedy approach. Follow the steps mentioned below to solve the problem:
Below is the implementation of the given approach.
ababcababcd
Time Complexity: O(N)
Auxiliary Space: O(N)