通过将所有 * 替换为编码字符串的前缀值来解密给定代码中的消息
给定一个长度为N的字符串str ,它采用字母和*的编码形式。任务是找到生成它的字符串。通过将所有*替换为编码字符串的前缀值,可以从编码字符串生成所需的字符串。
例子:
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
方法:解决方案基于贪婪方法。请按照以下步骤解决问题:
- 考虑一个空字符串result 。
- 遍历给定的编码字符串。
- 如果字符串中的当前字符不是“*” ,则将当前字符添加到结果中。
- 否则,如果当前字符为“*”,则将目前形成的结果字符串与自身相加。
- 返回结果。
下面是给定方法的实现。
C++
// C++ code to implement above approach
#include
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
输出
ababcababcd
时间复杂度: O(N)
辅助空间: O(N)