Perform String Shifts (LeetCode Day 14 Question) | Programming Tutorials

preview_player
Показать описание


Given a string s containing lowercase English letters, and a matrix shift,
where shift[i] = [direction, amount]:

** Direction can be 0 (for left shift) or 1 (for right shift).
** Amount is the amount by which string s is to be shifted.
** A left shift by 1 means remove the first character of s and append it to the end.
** Similarly, a right shift by 1 means remove the last character of s and add it to the beginning.

Return the final string after all operations.

For Example -

Example 1:

Input: s = "abc", shift = [[0,1], [1,2]]
Output: "cab"

Explanation:
[0,1] means shift to left by 1. "abc" == "bca"
[1,2] means shift to right by 2. "bca" == "cab"



Example 2:

Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]]
Output: "efgabcd"


Explanation:
[1,1] means shift to right by 1. "abcdefg" == "gabcdef"
[1,1] means shift to right by 1. "gabcdef" == "fgabcde"
[0,2] means shift to left by 2. "fgabcde" == "abcdefg"
[1,3] means shift to right by 3. "abcdefg" == "efgabcd"

This problem is the day 14 challenge of LeetCode 30 day challenge.

Рекомендации по теме
Комментарии
Автор

Thanks for your efforts and very clean code.Good explanation.Amazing videos.
Based on your code, we can make improvement.


Instead of shifting string for each index. Just calculate Total Left Shifts(TLS) and Calculate Total Right shits(TRS) . Find difference between TLS and TRS . Perform shift at once.


public static String stringShift(String s, int[][] shift) {
int leftShift = 0, rightShift = 0;
int numberOfShifts = 0;
String newString = null;

for (int[] a : shift) {
if (a[0] == 0) {
leftShift = leftShift + a[1];
} else {
rightShift = rightShift + a[1];
}
}

if (leftShift > rightShift) {
numberOfShifts = leftShift - rightShift;
numberOfShifts = numberOfShifts % s.length();
String left = s.substring(0, numberOfShifts);
newString =
} else if (rightShift > leftShift) {
numberOfShifts = rightShift - leftShift;
numberOfShifts = (numberOfShifts % s.length());
String right = s.substring(s.length() - numberOfShifts);
newString = right.concat(s.substring(0, s.length()-numberOfShifts));
}
return newString;
}

praveenpraveen
Автор

Your videos are really usefull and explanatiions are good thanks

rakesht.j
Автор

good one, java version
class Solution {
public String stringShift(String s, int[][] shift) {
int moves=0;

for(int[] move:shift) {
if(move[0]==0)
moves += move[1];
else
moves -= move[1];
}

moves = moves % s.length();
if(moves>0)
return s.substring(moves, s.length())+s.substring(0, moves);
else
return s.substring(moves+s.length(), s.length())+s.substring(0, moves+s.length());
}
}

nagalakshmi
Автор

Solution:


public class Solution {
public static String stringShift(String S, int[][] shift) {
int len = S.length();

for (int [] value: shift) {
if (value[0] == 0) {
S = leftShift(S, value[1] % len);
}
else {
S = rightShift(S, value[1] % len);
}
}
return S;
}

public static String leftShift(String S, int num) {
return S.substring(num) + S.substring(0, num);
}

public static String rightShift(String S, int num) {
return S.substring(S.length() - num) + S.substring(0, S.length() - num);
}
}

anamusib
Автор

Why do you use substring method ? Why not StringBuilder ?

Piyush-kyee
join shbcf.ru