Skip to main content
53.Reverse the String According to its Index
Problem of the Day:
You are given a string s and an integer k. Your task is to transform the string in the following way:
Split the string into groups of size k, starting from index 0.
For each group: Rotate the group to the left by 1 character (the first character moves to the end).
If the last group has fewer than k characters, still apply the same operation.
Finally, return the new transformed string.
- Input 1:
String str1=ZohoCorporations
k=3
Output1: hoZoCooprtarnois
- Input 2:
String str1=Hellokid
k=2
Output2:eHllkodi
class Prob1
ReplyDelete{
public static void main(String[]args)
{
String s = "Zohocorporations"; int k = 25;
String str = "";
for(int i = 0;i0 && i%k == 0)
{
System.out.print(str);
str = "";
}
str = s.charAt(i) + str;
}
if(str.length()>0)
{
System.out.println(str);
}
}
}