📌  相关文章
📜  用不同的邻居将给定二进制字符串中的所有 0 翻转 K 次

📅  最后修改于: 2022-05-13 01:56:10.186000             🧑  作者: Mango

用不同的邻居将给定二进制字符串中的所有 0 翻转 K 次

给定一个大小为N的二进制字符串S和一个正整数K ,任务是通过在每次迭代中翻转所有具有不同相邻字符的0来重复修改给定字符串K次。

例子:

方法:给定问题可以通过执行给定操作K次来解决,然后打印形成的结果字符串。请按照以下步骤解决此问题:

  • 使用变量i迭代范围[0, K – 1]并执行以下步骤:
    • 使用变量j[0, N – 1]范围内遍历给定的字符串S并执行以下步骤:
      • 如果第0索引处的字符为0 ,则将此值替换为第一个 指数值。
      • 否则,如果字符0出现在最后一个索引处,则将此值替换为倒数第二个索引字符。
      • 否则,如果相邻字符不同,则将所有0转换为1
    • 经过上述步骤,如果字符串在修改前保持不变,则跳出循环。
  • 完成上述步骤后,打印字符串S作为修改后的字符串。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to modify the given string
// K number of times by flipping 0s
// having different adjacent characters
void convertString(string S, int k)
{
    // Size of the string
    int n = S.length();
 
    // Stores modified string after
    // each iteration
    string temp = S;
 
    // Iterate over the range [0 k]
    for (int i = 0; i < k; i++) {
 
        // Traverse the string S
        for (int j = 0; j < n; j++) {
 
            // If '0' is present at
            // 0th index then replace
            // it with 1st index
            if (j == 0 && S[j] == '0') {
 
                temp[j] = S[j + 1];
            }
 
            // If '0' is present at the
            // last index then replace
            // it with last index - 1
            // character
            else if (j == n - 1
                     && S[j] == '0') {
 
                temp[j] = S[j - 1];
            }
 
            // Otherwise, convert 0s
            // to 1 if the adjacent
            // characters are different
            else if (S[j - 1] != S[j + 1]
                     && S[j] == '0') {
 
                temp[j] = '1';
            }
        }
 
        // If during this iteration
        // there is no change in the
        // string then break this loop
        if (S == temp) {
            break;
        }
 
        // Update the string S
        S = temp;
    }
 
    // Print the updated string
    cout << S;
}
 
// Driver Code
int main()
{
    string S = "10010001";
    int K = 1;
    convertString(S, K);
 
    return 0;
}


Python3
# python 3 program for the above approach
 
# Function to modify the given string
# K number of times by flipping 0s
# having different adjacent characters
def convertString(S, k):
    # Size of the string
    n = len(S)
 
    # Stores modified string after
    # each iteration
    temp = S
    temp = list(temp)
 
    # Iterate over the range [0 k]
    for i in range(k):
        # Traverse the string S
        for j in range(n-1):
            # If '0' is present at
            # 0th index then replace
            # it with 1st index
            if (j == 0 and S[j] == '0'):
 
                temp[j] = S[j + 1]
 
            # If '0' is present at the
            # last index then replace
            # it with last index - 1
            # character
            elif (j == n - 1 and S[j] == '0'):
 
                temp[j] = S[j - 1]
 
            # Otherwise, convert 0s
            # to 1 if the adjacent
            # characters are different
            elif (S[j - 1] != S[j + 1] and S[j] == '0'):
                temp[j] = '1'
 
        # If during this iteration
        # there is no change in the
        # string then break this loop
        if (S == temp):
            break
        temp = ''.join(temp)
        # Update the string S
        S = temp
     
 
    # Print the updated string
    print(S)
 
# Driver Code
if __name__ == '__main__':
    S = "10010001"
    K = 1
    convertString(S, K)
     
    # This code s contributed by ipg2016107.


Javascript


时间复杂度: O(N*K)
辅助空间: O(N)