Reputation: 35
I'm new to Stack Overflow and I have a lab question for a programming class that's been eluding me. The problem requires us to shift the elements of a string s to the left k times. For instance, if the input is "Hello World" and 3, it would output "lo WorldHel"). It also has to work relatively efficiently for very large values of k. This is what I have so far:
String cyclicLeftShift(String s, int k){
String result="";
for(int i=0;i<k;i++){
result = s.substring(1, s.length() - 1) +s.charAt(0);
s=result;
}
return s;
}
My major issue is that the last character of the original string keeps getting overwritten by the subsequent iterations of the loop. I've tried a great number of permutations, including converting the whole thing to arrays (which violates the efficiency restriction in the original problem). I feel like there's just a tiny thing I'm not getting, and I was wondering if someone could give me a nudge in the right direction?
Thank you!
Upvotes: 3
Views: 23420
Reputation: 372
A quick solution, just need to take care that the val to be shifted must not be OutOfBounds.
String shiftLeft(String inp, int val)
{
String shifted_str="";
shifted_str=inp.substring(val);
shifted_str+=inp.substring(0,val);
return shifted_str;
}
Upvotes: 0
Reputation: 75
Not sure if this solution is of help ! But it worked for me :) Well you could try it out!
class Main {
public static void main(String[] args) {
System.out.println("Moving left by n characters");
String str1 = moveCHaracters("Hellow World", 4);
System.out.println(str1);
}
public static String moveCHaracters(String s, int k) {
String result = s.substring(s.length() - (k));
int length = s.length() - k;
if (k > 0) {
for (int i = 0; i < length; i++) {
result = result + s.charAt(i);
}
}
return result;
}
}
Upvotes: 0
Reputation: 14941
You can try this:
public static String cyclicLeftShift(String s, int k){
String result=s;
for(int i=0; i<k; i++){
result = result.substring(1) + result.charAt(0);
}
return result;
}
Here's an example via TutorialsPoint. Just click on compile then execute to see the result.
Upvotes: 1
Reputation: 5680
What you want is to split the string at position k
and merge both parts together again but in reverse order.
The main problem is that k
may be greater than or equal to the size of your string. So you need to bring k
into a valid range again.
public static String cyclicLeftShift(String s, int k){
k = k%s.length();
return s.substring(k) + s.substring(0, k);
}
Testing the method:
public static void main(String[] args)
{
String test = "Hello World";
for(int i = 0; i < test.length()*3; i++)
System.out.println(cyclicLeftShift(test, i));
}
Output:
Hello World
ello WorldH
llo WorldHe
lo WorldHel
o WorldHell
WorldHello
WorldHello
orldHello W
rldHello Wo
ldHello Wor
dHello Worl
Hello World
ello WorldH
llo WorldHe
lo WorldHel
o WorldHell
WorldHello
WorldHello
orldHello W
rldHello Wo
ldHello Wor
dHello Worl
Hello World
ello WorldH
llo WorldHe
lo WorldHel
o WorldHell
WorldHello
WorldHello
orldHello W
rldHello Wo
ldHello Wor
dHello Worl
Upvotes: 5
Reputation: 59273
The arguments to String.substring() are (beginIndex, endIndex), NOT (beginIndex, count). You need to pass s.length() instead of s.length()-1... Or you could do it one of the much faster ways that others are posting
Upvotes: 0
Reputation: 396
Maybe I'm missing something, but can you not just mod k by the length of s to get n (number of characters to shift), then take the substring of [0,n) and append it to the substring [n, s.length() -1]?
e.g.:
String cyclicLeftShift(String s, int k){
String result="";
int n = k % s.length();
result = s.substring(n) + s.substring(0,n);
return result;
}
Upvotes: 1
Reputation: 2526
Try this one my boy:
String cyclicLeftShift(String s, int k) {
String result = s.substring(k);
for (int i = 0; i < k; i++) {
result += s.charAt(i);
}
return result;
}
Upvotes: 1