本文概述
- C ++
- Java
- Python3
- C#
例子:
输入:str ="abbaaa" K = 2【生成一个字符串,其所有K大小的子字符串都可以串联形成给定的字符串】方法:
输出:abaa
说明:
父字符串"abaa"的大小为2的所有子字符串均为"ab", "ba"和"aa"。连接所有这些子字符串后, 可以获得给定的字符串"abbaaa"。
输入:str ="abcbcscsesesesd" K = 3
输出:abcsesd
说明:
父字符串"abcsesd"的大小为3的所有子字符串均为"abc", "bcs", "cse", "ses"和"esd"。连接所有这些子字符串后, 可以获得给定的字符串"abcbcscsesesesd"。
请按照以下步骤解决问题:
- 我们可以通过串联长度子串来清楚地观察到?, 除了第一个字符外, 其余K-1下一个子字符串中也将包含任何子字符串的字符。
- 因此, 遍历字符串并将每个子字符串的第一个字符附加到安斯然后忽略下一步K-1字符。
- 对除最后一个子字符串以外的所有子字符串重复此过程。
- 将最后一个子字符串的所有字符附加到安斯.
- 返回ans作为所需的解码字符串。
C ++
//C++ program to generate a
//string whose substrings of
//length K concatenates to
//form given strings#include <
bits/stdc++.h>
using namespace std;
//Function to return the required
//required string
void decode_String(string str, int K)
{
string ans = "" ;
//Iterate the given string
for ( int i = 0;
i <
str.size();
i += K)
//Append the first
//character of every
//substring of length K
ans += str[i];
//Consider all characters
//from the last substring
for ( int i = str.size() - (K - 1);
i <
str.size();
i++)
ans += str[i];
cout <
<
ans <
<
endl;
}//Driver Program
int main()
{
int K = 3;
string str = "abcbcscsesesesd" ;
decode_String(str, K);
}
Java
//Java program to generate a
//string whose substrings of
//length K concatenates to
//form given strings
class GFG{//Function to return the required
//required string
public static void decode_String(String str, int K)
{
String ans = "" ;
//Iterate the given string
for ( int i = 0 ;
i <
str.length();
i += K) //Append the first
//character of every
//substring of length K
ans += str.charAt(i);
//Consider all characters
//from the last substring
for ( int i = str.length() - (K - 1 );
i <
str.length();
i++)
ans += str.charAt(i);
System.out.println(ans);
} //Driver code
public static void main(String[] args)
{
int K = 3 ;
String str = "abcbcscsesesesd" ;
decode_String(str, K);
}
}//This code is contributed by divyeshrabadiya07
Python3
# Python3 program to generate a
# string whose substrings of
# length K concatenates to
# form given strings# Function to return the required
# required string
def decode_String(st, K):ans = ""# Iterate the given string
for i in range ( 0 , len (st), K):# Append the first
# character of every
# substring of length K
ans + = st[i]# Consider all characters
# from the last substring
for i in range ( len (st) - (K - 1 ), len (st)):
ans + = st[i]print (ans)# Driver code
if __name__ = = "__main__" :K = 3
st = "abcbcscsesesesd"decode_String(st, K)# This code is contributed by chitranayal
C#
//C# program to generate a string
//whose substrings of length K
//concatenates to form given strings
using System;
class GFG{//Function to return the required
//required string
public static void decode_String(String str, int K)
{
String ans = "" ;
//Iterate the given string
for ( int i = 0;
i <
str.Length;
i += K) //Append the first
//character of every
//substring of length K
ans += str[i];
//Consider all characters
//from the last substring
for ( int i = str.Length - (K - 1);
i <
str.Length;
i++)
ans += str[i];
Console.WriteLine(ans);
} //Driver code
public static void Main(String[] args)
{
int K = 3;
String str = "abcbcscsesesesd" ;
decode_String(str, K);
}
}//This code is contributed by Rohit_ranjan
输出如下:
abcsesd
时间复杂度:O(n)
辅助空间:O(1)
推荐阅读
- 光学计算概论(介绍、优点和缺点)
- 给定字符串中频率为K的每个不同字符的最大索引
- 使用数组从质因数只有2和3的范围中计数数字|S2
- 最新win1064位旗舰版制作详细说明
- 本图文详细教程教你win10系统怎样还原为win764位旗舰版系
- 深度技术windows1064位旗舰版系统最新推荐
- windows10企业版系统激活工具最新推荐
- 最新win10激活工具图文详细教程图解
- 看完你就知道win10系统怎样样制作详细说明