Decrypt message from given code by replacing all * with prefix values of encoded string
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:
- Consider an empty string result.
- Iterate over the given coded string.
- if the current character in the string is not “*” then add the current character to the result.
- Otherwise, if the current character is “*”, add the result string formed till now with itself.
- Return the result.
Below is the implementation of the given approach.
C++
// C++ code to implement above approach #include <bits/stdc++.h> using namespace std; // Function to return a string // found from the coded string string findstring(string str) { // Declaring string to store result string result = "" ; // Loop to generate the original string for ( int i = 0; str[i] != '\0' ; i++) { // If current character in string // is '*' add result to itself if (str[i] == '*' ) result += result; // Else add current element only else result += str[i]; } // Return the result return result; } // Driver code int main() { string str = "ab*c*d" ; cout << findstring(str); return 0; } |
Java
// Java code to implement above approach class GFG { // Function to return a string // found from the coded string static String findstring(String str) { // Declaring string to store result String result = "" ; // Loop to generate the original string for ( int i = 0 ; i < str.length(); i++) { // If current character in string // is '*' add result to itself if (str.charAt(i) == '*' ) result += result; // Else add current element only else result += str.charAt(i); } // Return the result return result; } // Driver code public static void main(String[] args) { String str = "ab*c*d" ; System.out.println(findstring(str)); } } // This code is contributed by ukasp. |
Python3
# Python code for the above approach # Function to return a string # found from the coded string def findstring( str ): # Declaring string to store result result = "" # Loop to generate the original string for i in range ( len ( str )): # If current character in string # is '*' add result to itself if ( str [i] = = '*' ): result + = result # Else add current element only else : result + = str [i] # Return the result return result # Driver code str = "ab*c*d" print (findstring( str )) # This code is contributed by Saurabh Jaiswal |
C#
// C# code to implement above approach using System; class GFG { // Function to return a string // found from the coded string static string findstring( string str) { // Declaring string to store result string result = "" ; // Loop to generate the original string for ( int i = 0; i < str.Length; i++) { // If current character in string // is '*' add result to itself if (str[i] == '*' ) result += result; // Else add current element only else result += str[i]; } // Return the result return result; } // Driver code public static void Main() { string str = "ab*c*d" ; Console.Write(findstring(str)); } } // This code is contributed by Samim Hossain Mondal. |
Javascript
<script> // JavaScript code for the above approach // Function to return a string // found from the coded string function findstring(str) { // Declaring string to store result let result = "" ; // Loop to generate the original string for (let i = 0; i < str.length; i++) { // If current character in string // is '*' add result to itself if (str[i] == '*' ) result += result; // Else add current element only else result += str[i]; } // Return the result return result; } // Driver code let str = "ab*c*d" ; document.write(findstring(str)); // This code is contributed by Potta Lokesh </script> |
ababcababcd
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...