反转字符串中的交替 k 个字符
给定一个字符串str和一个整数k ,任务是反转给定字符串的交替k个字符。如果出现的字符小于 k,则保持原样。
例子:
Input: str = “geeksforgeeks”, k = 3
Output: eegksfgroeeks
Input: str = “abcde”, k = 2
Output: bacde
方法:想法是首先反转k个字符,然后通过将2 * k添加到索引等来跳转到下一个k个字符,依此类推,直到修改完整的字符串。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the string after
// reversing the alternate k characters
string revAlternateK(string s, int k, int len)
{
for (int i = 0; i < s.size();) {
// If there are less than k characters
// starting from the current position
if (i + k > len)
break;
// Reverse first k characters
reverse(s.begin() + i, s.begin() + i + k);
// Skip the next k characters
i += 2 * k;
}
return s;
}
// Driver code
int main()
{
string s = "geeksforgeeks";
int len = s.length();
int k = 3;
cout << revAlternateK(s, k, len);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the string after
// reversing the alternate k characters
static String revAlternateK(String s,
int k, int len)
{
for (int i = 0; i < s.length();)
{
// If there are less than k characters
// starting from the current position
if (i + k > len)
break;
// Reverse first k characters
s = s.substring(0, i) + new String(new StringBuilder(
s.substring(i, i + k)).reverse()) +
s.substring(i + k);
// Skip the next k characters
i += 2 * k;
}
return s;
}
// Driver code
public static void main(String[] args)
{
String s = "geeksforgeeks";
int len = s.length();
int k = 3;
System.out.println(revAlternateK(s, k, len));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to return the string after
# reversing the alternate k characters
def revAlternateK(s, k, Len):
i = 0
while(i < len(s)):
# If there are less than k characters
# starting from the current position
if (i + k > Len):
break
# Reverse first k characters
ss = s[i:i + k]
s = s[:i]+ss[::-1]+s[i + k:]
# Skip the next k characters
i += 2 * k
return s;
# Driver code
s = "geeksforgeeks"
Len = len(s)
k = 3
print(revAlternateK(s, k, Len))
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the string after
// reversing the alternate k characters
static String revAlternateK(String s,
int k, int len)
{
for (int i = 0; i < s.Length;)
{
// If there are less than k characters
// starting from the current position
if (i + k > len)
break;
// Reverse first k characters
s = s.Substring(0, i) +
reverse(s.Substring(i, k).ToCharArray(), 0, k - 1) +
s.Substring(i + k);
// Skip the next k characters
i += 2 * k;
}
return s;
}
static String reverse(char []str, int start, int end)
{
// Temporary variable to store character
char temp;
while (start <= end)
{
// Swapping the first and last character
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
return String.Join("", str);
}
// Driver code
public static void Main(String[] args)
{
String s = "geeksforgeeks";
int len = s.Length;
int k = 3;
Console.WriteLine(revAlternateK(s, k, len));
}
}
// This code contributed by Rajput-Ji
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the string after
// reversing the alternate K characters
string revAlternateK(string s, int k, int n)
{
string ans = "";
int i = 0, j = 0;
// Traverse till the end
// of original string
while (i < n) {
// Traverse backwards from i+k to i
// and store the characters
// in resultant string
for (j = min(i + k, n) - 1; j >= i; j--)
ans += s[j];
i = min(i + k, n);
// Traverse from i to i+k and store
// the characters in resulant string
for (j = i; j < min(i + k, n); j++)
ans += s[j];
i = j;
}
// Return ans
return ans;
}
// Driver code
int main()
{
string str = "geeksforgeeks";
int N = str.length();
int K = 3;
cout << revAlternateK(str, K, N);
return 0;
}
输出
eegksfgroeeks
方法二:
The idea to solve this problem is to traverse the string, and while traversing reverse first K characters, then skip next K characters, then again reverse next K characters and so on until the complete string is modified.
请按照以下步骤解决问题:
- 遍历到原始字符串的末尾
- 从 i+k 向后遍历到 i 并将字符存储在结果字符串中
- 将 i 更新为 i+k
- 现在从 i 遍历到 i + k,并将字符存储在结果字符串中
- 返回原始字符串
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the string after
// reversing the alternate K characters
string revAlternateK(string s, int k, int n)
{
string ans = "";
int i = 0, j = 0;
// Traverse till the end
// of original string
while (i < n) {
// Traverse backwards from i+k to i
// and store the characters
// in resultant string
for (j = min(i + k, n) - 1; j >= i; j--)
ans += s[j];
i = min(i + k, n);
// Traverse from i to i+k and store
// the characters in resulant string
for (j = i; j < min(i + k, n); j++)
ans += s[j];
i = j;
}
// Return ans
return ans;
}
// Driver code
int main()
{
string str = "geeksforgeeks";
int N = str.length();
int K = 3;
cout << revAlternateK(str, K, N);
return 0;
}
输出
eegksfgroeeks
时间复杂度:O(N)
辅助空间:O(N)