Javascript Program to Modify a string by performing given shift operations
Last Updated :
20 Jul, 2022
Given a string S containing lowercase English alphabets, and a matrix shift[][] consisting of pairs of the form{direction, amount}, where the direction can be 0 (for left shift) or 1 (for right shift) and the amount is the number of indices by which the string S is required to be shifted. The task is to return the modified string that can be obtained after performing the given operations.
Note: A left shift by 1 refers to removing the first character of S and append it to the end. Similarly, a right shift by 1 refers to removing the last character of S and insert at the beginning.
Examples
Input: S = "abc", shift[][] = {{0, 1}, {1, 2}}
Output: cab
Explanation:
[0, 1] refers to shifting S[0] to the left by 1. Therefore, the string S modifies from "abc" to "bca".
[1, 2] refers to shifting S[0] to the right by 1. Therefore, the string S modifies from "bca"to "cab".
Input: S = "abcdefg", shift[][] = { {1, 1}, {1, 1}, {0, 2}, {1, 3} }
Output: efgabcd
Explanation:
[1, 1] refers to shifting S[0] to the right by 1. Therefore, the string S modifies from "abcdefg" to "gabcdef".
[1, 1] refers to shifting S[0] to the right by 1. Therefore, the string S modifies from "gabcdef" to "fgabcde".
[0, 2] refers to shifting S[0] to the left by 2. Therefore, the string S modifies from "fgabcde" to "abcdefg".
[1, 3] refers to shifting S[0] to the right by 3. Therefore, the string S modifies from "abcdefg" to "efgabcd".
Naive Approach: The simplest approach to solve the problem is to traverse the matrix shift[][] and shift S[0] by amount number of indices in the specified direction. After completing all shift operations, print the final string obtained.
Time Complexity: O(N2)
Auxiliary space: O(N)
Efficient Approach: To optimize the above approach, follow the steps below:
- Initialize a variable, say val, to store the effective shifts.
- Traverse the matrix shift[][] and perform the following operations on every ith row:
- If shift[i][0] = 0 (left shift), then decrease val by -shift[i][1].
- Otherwise (left shift), increase val by shift[i][1].
- Update val = val % len (for further optimizing the effective shifts).
- Initialize a string, result = "", to store the modified string.
- Now, check if val > 0. If found to be true, then perform the right rotation on the string by val.
- Otherwise, perform left rotation of the string by |val| amount.
- Print the result.
Below is the implementation of the above approach:
JavaScript
<script>
// Javascript implementation
// of above approach
// Function to find the string obtained
// after performing given shift operations
function stringShift(s, shift)
{
var val = 0;
for (var i = 0; i < shift.length; ++i)
// If shift[i][0] = 0, then left shift
// Otherwise, right shift
val += shift[i][0] == 0
? -shift[i][1]
: shift[i][1];
// Stores length of the string
var len = s.length;
// Effective shift calculation
val = val % len;
// Stores modified string
var result = "";
// Right rotation
if (val > 0)
result = s.substring(len - val,len)
+ s.substring(0, len - val);
// Left rotation
else
result
= s.substring(-val, len )
+ s.substring(0, -val);
document.write( result);
}
// Driver Code
var s = "abc";
var shift = [
[ 0, 1 ],
[ 1, 2 ]
];
stringShift(s, shift);
// This code is contributed by importantly.
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Please refer complete article on Modify a string by performing given shift operations for more details!
Similar Reads
Modify a string by performing given shift operations Given a string S containing lowercase English alphabets, and a matrix shift[][] consisting of pairs of the form{direction, amount}, where the direction can be 0 (for left shift) or 1 (for right shift) and the amount is the number of indices by which the string S is required to be shifted. The task i
6 min read
Javascript Program for Left Rotation and Right Rotation of a String Given a string of size n, write functions to perform the following operations on a string-Left (Or anticlockwise) rotate the given string by d elements (where d <= n)Right (Or clockwise) rotate the given string by d elements (where d <= n).Examples: Input : s = "GeeksforGeeks" d = 2Output : Le
3 min read
Javascript Program to Minimize characters to be changed to make the left and right rotation of a string same Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = âabcdâOutput: 2Explanation:String after the left shift: âbcdaâString after the right shift: âdabc
3 min read
Javascript Program to Check if a string can be formed from another string by at most X circular clockwise shifts Given an integer X and two strings S1 and S2, the task is to check that string S1 can be converted to the string S2 by shifting characters circular clockwise atmost X times. Input: S1 = "abcd", S2 = "dddd", X = 3 Output: Yes Explanation: Given string S1 can be converted to string S2 as- Character "a
3 min read
Move all digits to the beginning of a given string Given a string S, the task is to move all the digits present in the string, to the beginning of the string. Examples: Input: S = âGeeks4forGeeks123âOutput: 4123GeeksforGeeksExplanation:The given string contains digits 4, 1, 2, and 3. Moving all the digits to the beginning of the string modifies the
7 min read
Left Rotation of a String Given a string s and an integer d, the task is to left rotate the string by d positions.Examples:Input: s = "GeeksforGeeks", d = 2Output: "eksforGeeksGe" Explanation: After the first rotation, string s becomes "eeksforGeeksG" and after the second rotation, it becomes "eksforGeeksGe".Input: s = "qwer
15+ min read