给定一个包含小写英文字母的字符串S和一个由{direction, amount}形式的对组成的矩阵shift[][] ,其中方向可以是0(左移)或1(右移)和数量是字符串S需要移位的索引数。任务是返回执行给定操作后可以获得的修改后的字符串。
注意:左移 1 是指删除S的第一个字符并将其附加到末尾。同样,右移 1 是指删除S的最后一个字符并在开头插入。
例子
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”.
朴素方法:解决问题的最简单方法是遍历矩阵shift[][]并将 S[0] 移动指定方向的索引数量。完成所有移位操作后,打印得到的最终字符串。
时间复杂度: O(N 2 )
辅助空间: O(N)
高效的方法:要优化上述方法,请按照以下步骤操作:
- 初始化一个变量,比如val,以存储有效的移位。
- 遍历矩阵shift[][]并对每第i 行执行以下操作:
- 如果shift[i][0] = 0 ( left shift ) ,则将val 减少 -shift[i][1]。
- 除此以外 (左移),通过shift[i][1]增加val 。
- 更新val = val % len (用于进一步优化有效偏移)。
- 初始化一个字符串result = “” ,以存储修改后的字符串。
- 现在,检查是否 val > 0 。如果发现为真,则通过val对字符串执行正确的旋转。
- 否则,按|val|执行字符串的左旋转数量。
- 打印结果。
下面是上述方法的实现:
C++
// C++ implementataion
// of above approach
#include
using namespace std;
// Function to find the string obtained
// after performing given shift operations
void stringShift(string s,
vector >& shift)
{
int val = 0;
for (int i = 0; i < shift.size(); ++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
int len = s.length();
// Effective shift calcuation
val = val % len;
// Stores modified string
string result = "";
// Right rotation
if (val > 0)
result = s.substr(len - val, val)
+ s.substr(0, len - val);
// Left rotation
else
result
= s.substr(-val, len + val)
+ s.substr(0, -val);
cout << result;
}
// Driver Code
int main()
{
string s = "abc";
vector > shift = {
{ 0, 1 },
{ 1, 2 }
};
stringShift(s, shift);
return 0;
}
Java
// Java implementataion
// of above approach
import java.io.*;
class GFG
{
// Function to find the string obtained
// after performing given shift operations
static void stringShift(String s, int[][] shift)
{
int val = 0;
for (int i = 0; i < shift.length; ++i)
// If shift[i][0] = 0, then left shift
// Otherwise, right shift
if (shift[i][0] == 0)
val -= shift[i][1];
else
val += shift[i][1];
// Stores length of the string
int len = s.length();
// Effective shift calcuation
val = val % len;
// Stores modified string
String result = "";
// Right rotation
if (val > 0)
result = s.substring(len - val, (len - val) + val)
+ s.substring(0, len - val);
// Left rotation
else
result = s.substring(-val, len + val)
+ s.substring(0, -val);
System.out.println(result);
}
// Driver Code
public static void main(String[] args)
{
String s = "abc";
int[][] shift
= new int[][] {{ 0, 1 }, { 1, 2 }};
stringShift(s, shift);
}
}
// This code is contributed by Dharanendra L V
C#
// C# implementataion
// of above approach
using System;
public class GFG
{
// Function to find the string obtained
// after performing given shift operations
static void stringShift(String s, int[,] shift)
{
int val = 0;
for (int i = 0; i < shift.GetLength(0); ++i)
// If shift[i,0] = 0, then left shift
// Otherwise, right shift
if (shift[i,0] == 0)
val -= shift[i, 1];
else
val += shift[i, 1];
// Stores length of the string
int len = s.Length;
// Effective shift calcuation
val = val % len;
// Stores modified string
String result = "";
// Right rotation
if (val > 0)
result = s.Substring(len - val, val)
+ s.Substring(0, len - val);
// Left rotation
else
result = s.Substring(-val, len)
+ s.Substring(0, -val);
Console.WriteLine(result);
}
// Driver Code
public static void Main(String[] args)
{
String s = "abc";
int[,] shift
= new int[,] {{ 0, 1 }, { 1, 2 }};
stringShift(s, shift);
}
}
// This code contributed by shikhasingrajput
Javascript
cab
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live