📌  相关文章
📜  根据指定的数组索引反转给定字符串的子字符串

📅  最后修改于: 2021-04-23 09:11:09             🧑  作者: Mango

给定长度为N的字符串str和整数数组arr [] ,对于数组元素arr [i](基于1的索引) ,将子字符串反转为索引[arr [i],N – arr [i] + 1] 。任务是在每次反转后打印字符串。

例子:

天真的方法:最简单的方法是遍历给定的数组,并对每个数组元素arr [i]反转子字符串{s [arr [i]],…s [N – arr [i] + 1]}并打印结果非常更新后获得的字符串。

时间复杂度: O(N * K),其中N是字符串的长度,K是反转的子字符串的最大长度。
辅助空间: O(1)

高效方法:可以通过跟踪索引处任何字符被反转的次数来优化上述方法。如果反转次数为偶数,则字符将返回其原始位置,因此不会有任何变化,并且如果反转次数为奇数,则必须交换字符。步骤如下:

  • 初始化数组count []以存储在字符串的任何索引处的反转次数。
  • 遍历给定数组arr []并将索引count [arr [i]]的计数增加1
  • 现在,使用变量i[1,N / 2]范围内遍历数组count []并执行以下操作:
    • 将当前索引处的元素更新为当前索引与先前索引的和。
    • 现在,如果当前元素count [i]为奇数,则交换str [i]str [N – i + 1]
  • 完成上述步骤后,打印字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to perform the reversal
// operation on the given string
string modifyString(int A[], string str,
                    int K)
{
    // Size of string
    int N = str.size();
 
    // Stores the count of indices
    int count[N + 1] = { 0 };
 
    // Count the positions where
    // reversals will begin
    for (int i = 0; i < K; i++) {
        count[A[i]]++;
    }
 
    for (int i = 1; i <= N / 2; i++) {
 
        // Store the count of reversals
        // beginning at position i
        count[i] = count[i] + count[i - 1];
 
        // Check if the count[i] is
        // odd the swap the character
        if (count[i] & 1) {
            swap(str[i - 1], str[N - i]);
        }
    }
 
    // Return the updated string
    return str;
}
 
// Driver Code
int main()
{
    // Given string str
    string str = "abcdef";
 
    // Given array of reversing index
    int arr[] = { 1, 2, 3 };
 
    int K = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    cout << modifyString(arr, str, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to perform the reversal
// operation on the given String
static String modifyString(int A[], String str,
                           int K)
{
     
    // Size of String
    int N = str.length();
 
    // Stores the count of indices
    int count[] = new int[N + 1];
 
    // Count the positions where
    // reversals will begin
    for(int i = 0; i < K; i++)
    {
        count[A[i]]++;
    }
 
    for(int i = 1; i <= N / 2; i++)
    {
         
        // Store the count of reversals
        // beginning at position i
        count[i] = count[i] + count[i - 1];
 
        // Check if the count[i] is
        // odd the swap the character
        if ((count[i] & 1) > 0)
        {
            str = swap(str, i - 1, N - i);
        }
    }
 
    // Return the updated String
    return str;
}
 
// Swap char of a string
static String swap(String str, int i, int j)
{
    char ch[] = str.toCharArray();
    char temp = ch[i];
    ch[i] = ch[j];
    ch[j] = temp;
    return String.valueOf(ch);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String str
    String str = "abcdef";
 
    // Given array of reversing index
    int arr[] = { 1, 2, 3 };
 
    int K = arr.length;
 
    // Function Call
    System.out.print(modifyString(arr, str, K));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to perform the reversal
# operation on the given string
def modifyString(A, str, K):
     
    # Size of string
    N = len(str)
 
    # Stores the count of indices
    count = [0] * (N + 1)
 
    # Count the positions where
    # reversals will begin
    for i in range(K):
        count[A[i]] += 1
 
    for i in range(1, N // 2 + 1):
         
        # Store the count of reversals
        # beginning at position i
        count[i] = count[i] + count[i - 1]
 
        # Check if the count[i] is
        # odd the swap the character
        if (count[i] & 1):
            str[i - 1], str[N - i] = str[N - i], str[i - 1]
 
    # Return the updated string
    return "".join(str)
 
# Driver Code
if __name__ == '__main__':
     
    # Given str
    str1 = "abcdef"
    str = [i for i in str1]
 
    # Given array of reversing index
    arr = [ 1, 2, 3 ]
 
    K = len(arr)
     
    # Function Call
    print(modifyString(arr, str, K))
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to perform the reversal
// operation on the given String
static String modifyString(int []A, String str,
                           int K)
{
     
    // Size of String
    int N = str.Length;
 
    // Stores the count of indices
    int []count = new int[N + 1];
 
    // Count the positions where
    // reversals will begin
    for(int i = 0; i < K; i++)
    {
        count[A[i]]++;
    }
 
    for(int i = 1; i <= N / 2; i++)
    {
         
        // Store the count of reversals
        // beginning at position i
        count[i] = count[i] + count[i - 1];
 
        // Check if the count[i] is
        // odd the swap the character
        if ((count[i] & 1) > 0)
        {
            str = swap(str, i - 1, N - i);
        }
    }
 
    // Return the updated String
    return str;
}
 
// Swap char of a string
static String swap(String str, int i, int j)
{
    char []ch = str.ToCharArray();
    char temp = ch[i];
    ch[i] = ch[j];
    ch[j] = temp; 
    return String.Join("", ch);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String str
    String str = "abcdef";
 
    // Given array of reversing index
    int []arr = { 1, 2, 3 };
 
    int K = arr.Length;
 
    // Function Call
    Console.Write(modifyString(arr, str, K));
}
}
 
// This code is contributed by Amit Katiyar


输出:
fbdcea











时间复杂度: O(N + K),其中N是字符串的长度,K是反转的子字符串的最大长度。
辅助空间: O(N)