Reverse alternate k characters in a string
Given a string str and an integer k, the task is to reverse alternate k characters of the given string. If characters present are less than k, leave them as it is.
Examples:
Input: str = “w3wiki”, k = 3
Output: eegksfgroeeks
Input: str = “abcde”, k = 2
Output: bacde
Approach: The idea is to first reverse k characters, then jump onto the next k characters by adding 2 * k to the index and so on until the complete string is modified.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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 = "w3wiki" ; 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 = "w3wiki" ; 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 = "w3wiki" 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 = "w3wiki" ; int len = s.Length; int k = 3; Console.WriteLine(revAlternateK(s, k, len)); } } // This code contributed by Rajput-Ji |
Javascript
<script> // Javascript implementation of the approach // Function to return the string after // reversing the alternate k characters function revAlternateK(s,k,len) { for (let 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) + s.substring(i, i + k).split( "" ).reverse().join( "" ) + s.substring(i + k); // Skip the next k characters i += 2 * k; } return s; } // Driver code let s = "w3wiki" ; let len = s.length; let k = 3; document.write(revAlternateK(s, k, len)); // This code is contributed by patel2127 </script> |
eegksfgroeeks
Time Complexity: O(N) where N is length of given string “s”
Auxiliary Space: O(1)
Approach 2:
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.
Follow the steps to solve the problem:
- Traverse till the end of original string
- Traverse backwards from i+k to i and store the characters in resultant string
- Update i to i+k
- Traverse from i to i + k now, and store the characters in resultant string
- Return the original string
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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 resultant 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 = "w3wiki" ; int N = str.length(); int K = 3; cout << revAlternateK(str, K, N); return 0; } |
Java
// Java implementation of the approach import java.io.*; class GFG { // Function to return the string after // reversing the alternate K characters static String revAlternate(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 = Math.min(i + k, n) - 1 ; j >= i; j--) { ans += s.charAt(j); } i = Math.min(i + k, n); // Traverse from i to i+k and store // the characters in resultant string for (j = i; j < Math.min(i + k, n); j++) { ans += s.charAt(j); } i = j; } // Return ans return ans; } public static void main(String[] args) { String str = "w3wiki" ; int N = str.length(); int K = 3 ; System.out.print(revAlternate(str, K, N)); } } // This code is contributed by lokeshmvs21. |
Python3
# Python3 implementation of the approach # Function to return the string after # reversing the alternate K characters def revAlternateK(s, k, n): ans = "" 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 in range ( min (i + k,n) - 1 ,i - 1 , - 1 ): ans + = s[j] i = min (i + k, n) # Traverse from i to i+k and store # the characters in resultant string for j in range (i, min (i + k,n)): ans + = s[j] i = j + 1 # Return ans return ans # Driver code str = "w3wiki" N = len ( str ) K = 3 print (revAlternateK( str , K, N)) # This code is contributed by akashish__ |
C#
// C# implementation of the approach using System; public class GFG { // Function to return the string after // reversing the alternate K characters static string revAlternate( 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 = Math.Min(i + k, n) - 1; j >= i; j--) { ans += s[j]; } i = Math.Min(i + k, n); // Traverse from i to i+k and store // the characters in resultant string for (j = i; j < Math.Min(i + k, n); j++) { ans += s[j]; } i = j; } // Return ans return ans; } static public void Main() { // Code string str = "w3wiki" ; int N = str.Length; int K = 3; Console.Write(revAlternate(str, K, N)); } } // This code is contributed by lokesh. |
Javascript
// JavaScript implementation of the approach // Function to return the string after // reversing the alternate K characters function revAlternate(s, k, n){ let ans = "" ; let 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 = Math.min(i + k, n) - 1; j >= i; j--) { ans += s[j]; } i = Math.min(i + k, n); // Traverse from i to i+k and store // the characters in resultant string for (j = i; j < Math.min(i + k, n); j++) { ans += s[j]; } i = j; } // Return ans return ans; } let str = "w3wiki" ; let N = str.length; let K = 3; console.log(revAlternate(str, K, N)); // This code is contributed by lokeshmvs21. |
eegksfgroeeks
Time Complexity: O(N)
Auxiliary Space: O(N)