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

📅  最后修改于: 2021-09-07 02:13:31             🧑  作者: 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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live