📌  相关文章
📜  相互替换(一个字符串的)两个子字符串

📅  最后修改于: 2021-04-21 23:01:05             🧑  作者: Mango

给定3个字符串SAB。的任务是,以取代S的每个子串等于ABS的每个子串等于B,其中A。匹配AB的两个或更多子字符串可能重叠。为避免这种情况造成混淆,您应该找到与AB匹配的最左边的子字符串,将其替换,然后继续其余的字符串。
例如,当将A =“ aa”S =“ aaa”匹配时, A [0,1]将优先于A [1,2]
请注意AB将具有相同的长度,并且A!= B。

例子:

方法:从长度为len(A)的S中遍历所有可能的子字符串。如果有任何子字符串与AB匹配,则根据需要更新该字符串,并在最后打印更新的字符串。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
// Function to return the resultant string
string updateString(string S, 
                    string A, string B)
{
  
    int l = A.length();
  
    // Iterate through all positions i
    for (int i = 0; i + l <= S.length(); i++)
    {
  
        // Current sub-string of 
        // length = len(A) = len(B)
        string curr = S.substr(i, i + l);
  
        // If current sub-string gets 
        // equal to A or B
        if (curr == A) 
        {
  
            // Update S after replacing A
            string new_string = "";
            new_string += S.substr(0, i) + B + 
                          S.substr(i + l, S.length());
            S = new_string;
            i += l - 1;
        }
        else
        {
  
            // Update S after replacing B
            string new_string = "";
            new_string += S.substr(0, i) + A + 
                          S.substr(i + l, S.length());
            S = new_string;
            i += l - 1;
        }
    }
  
    // Return the updated string
    return S;
}
  
// Driver code
int main()
{
    string S = "aab";
    string A = "aa";
    string B = "bb";
  
    cout << (updateString(S, A, B)) << endl;
}
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the resultant string
    static String updateString(String S, String A, String B)
    {
  
        int l = A.length();
  
        // Iterate through all positions i
        for (int i = 0; i + l <= S.length(); i++) {
  
            // Current sub-string of length = len(A) = len(B)
            String curr = S.substring(i, i + l);
  
            // If current sub-string gets equal to A or B
            if (curr.equals(A)) {
  
                // Update S after replacing A
                String new_string
                    = S.substring(0, i)
                      + B + S.substring(i + l, S.length());
                S = new_string;
                i += l - 1;
            }
            else {
  
                // Update S after replacing B
                String new_string
                    = S.substring(0, i)
                      + A + S.substring(i + l, S.length());
                S = new_string;
                i += l - 1;
            }
        }
  
        // Return the updated string
        return S;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String S = "aab";
        String A = "aa";
        String B = "bb";
  
        System.out.println(updateString(S, A, B));
    }
}


Python3
# Python3 implementation of the approach 
  
# Function to return the resultant string 
def updateString(S, A, B): 
      
    l = len(A) 
      
    # Iterate through all positions i 
    i = 0
    while i + l <= len(S): 
  
        # Current sub-string of 
        # length = len(A) = len(B) 
        curr = S[i:i+l] 
  
        # If current sub-string gets
        # equal to A or B 
        if curr == A: 
  
            # Update S after replacing A 
            new_string = S[0:i] + B + S[i + l:len(S)] 
            S = new_string 
            i += l - 1
              
        else:
  
            # Update S after replacing B 
            new_string = S[0:i] + A + S[i + l:len(S)] 
            S = new_string 
            i += l - 1
          
        i += 1
      
    # Return the updated string 
    return S 
  
# Driver code 
if __name__ == "__main__":
      
    S = "aab"
    A = "aa"
    B = "bb"
  
    print(updateString(S, A, B)) 
      
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach 
using System;
  
class GFG 
{ 
  
    // Function to return the resultant string 
    static string updateString(string S, string A, string B) 
    { 
        int l = A.Length; 
  
        // Iterate through all positions i 
        for (int i = 0; i + l <= S.Length; i++) 
        { 
  
            // Current sub-string of length = len(A) = len(B) 
            string curr = S.Substring(i, l); 
  
            // If current sub-string gets equal to A or B 
            if (curr.Equals(A))
            { 
  
                // Update S after replacing A 
                string new_string = S.Substring(0, i) +
                                 B + S.Substring(i + l); 
                S = new_string; 
                i += l - 1; 
            } 
            else 
            { 
  
                // Update S after replacing B 
                string new_string = S.Substring(0, i) +
                                A + S.Substring(i + l); 
                S = new_string; 
                i += l - 1; 
            } 
        } 
  
        // Return the updated string 
        return S; 
    } 
  
    // Driver code 
    public static void Main() 
    { 
        string S = "aab"; 
        string A = "aa"; 
        string B = "bb"; 
        Console.WriteLine(updateString(S, A, B)); 
    } 
} 
  
// This code is contributed by Ryuga


PHP


输出:
bbb