📌  相关文章
📜  通过仅设置一个 K 大小的子字符串位来最小化二进制字符串中的汉明距离

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

通过仅设置一个 K 大小的子字符串位来最小化二进制字符串中的汉明距离

给定两个长度为N的二进制字符串ST以及一个正整数K 。最初, T的所有字符都是'0' 任务是在选择一个大小为K的子串并使字符串T的所有元素为 “1”只有一次。

例子:

朴素方法:最简单的方法是考虑每个大小为K的子字符串,并将所有元素设为 1,然后使用字符串S检查汉明距离。检查所有子串后,打印最小汉明距离。

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

方法:这个问题可以通过创建一个前缀数组 sum 来解决,该数组存储字符串S中的个数的前缀和。请按照以下步骤解决问题:

  • 通过将pref[0]初始化为0将每个索引i更新为pref [i-1] +(S[i] – '0')来创建字符串S的前缀和数组pref[]
  • 将字符串S中的总数存储在变量cnt中。
  • 将变量ans初始化为cnt以存储所需的结果。
  • 使用变量i[0, NK]范围内迭代
    • 将变量val初始化为pref[i+K-1] – pref[i-1]以存储子字符串S[i, i+K-1]中的个数。
    • 创建两个变量AB来存储当前子串外的汉明距离和当前子串内的汉明距离,并用cnt - KBK - val初始化A。
    • ans(A + B)的最小值更新ans的值。
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find minimum Hamming
// Distance after atmost one operation
int minimumHammingDistance(string S, int K)
{
    // Store the size of the string
    int n = S.size();
 
    // Store the prefix sum of 1s
    int pref[n];
 
    // Create Prefix Sum array
    pref[0] = S[0] - '0';
    for (int i = 1; i < n; i++)
        pref[i] = pref[i - 1] + (S[i] - '0');
 
    // Initialize cnt as number of ones
    // in string S
    int cnt = pref[n - 1];
 
    // Store the required result
    int ans = cnt;
 
    // Traverse the string, S
    for (int i = 0; i < n - K; i++) {
 
        // Store the number of 1s in the
        // substring S[i, i+K-1]
        int value = pref[i + K - 1]
                    - (i - 1 >= 0 ? pref[i - 1] : 0);
 
        // Update the answer
        ans = min(ans, cnt - value + (K - value));
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
int main()
{
 
    // Given Input
    string s = "101";
    int K = 2;
 
    // Function Call
    cout << minimumHammingDistance(s, K);
 
    return 0;
}


Java
// Java program for the above approach
public class GFG
{
 
// Function to find minimum Hamming
// Distance after atmost one operation
static int minimumHammingDistance(String S, int K)
{
   
    // Store the size of the string
    int n = S.length();
 
    // Store the prefix sum of 1s
    int []pref =  new int [n];
 
    // Create Prefix Sum array
    pref[0] = S.charAt(0) - '0';
    for (int i = 1; i < n; i++)
        pref[i] = pref[i - 1] + (S.charAt(i) - '0');
 
    // Initialize cnt as number of ones
    // in string S
    int cnt = pref[n - 1];
 
    // Store the required result
    int ans = cnt;
 
    // Traverse the string, S
    for (int i = 0; i < n - K; i++) {
 
        // Store the number of 1s in the
        // substring S[i, i+K-1]
        int value = pref[i + K - 1] - (i - 1 >= 0 ? pref[i - 1] : 0);
 
        // Update the answer
        ans = Math.min(ans, cnt - value + (K - value));
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
public static void main(String args[])
{
    // Given Input
    String s = "101";
    int K = 2;
 
    // Function Call
    System.out.println(minimumHammingDistance(s, K));
    }
}
 
// This code is contributed by SoumikMondal


Python3
# Py program for the above approach
 
# Function to find minimum Hamming
# Distance after atmost one operation
def minimumHammingDistance(S, K):
    # Store the size of the string
    n = len(S)
 
    # Store the prefix sum of 1s
    pref = [0] * n
 
    # Create Prefix Sum array
    pref[0] = ord(S[0]) - ord('0')
    for i in range(1,n):
        pref[i] = pref[i - 1] + (ord(S[i]) - ord('0'))
 
    # Initialize cnt as number of ones
    # in string S
    cnt = pref[n - 1]
 
    # Store the required result
    ans = cnt
 
    # Traverse the string, S
    for i in range(n - K):
       
        # Store the number of 1s in the
        # substring S[i, i+K-1]
        value = pref[i + K - 1] - (pref[i-1] if (i - 1) >= 0 else 0)
 
        # Update the answer
        ans = min(ans, cnt - value + (K - value))
 
    # Return the result
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    # Given Input
    s = "101"
    K = 2
 
    # Function Call
    print (minimumHammingDistance(s, K))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG
{
 
// Function to find minimum Hamming
// Distance after atmost one operation
static int minimumHammingDistance(string S, int K)
{
   
    // Store the size of the string
    int n = S.Length;
 
    // Store the prefix sum of 1s
    int []pref =  new int [n];
 
    // Create Prefix Sum array
    pref[0] = (int)S[0] - 48;
    for (int i = 1; i < n; i++)
        pref[i] = pref[i - 1] + ((int)S[i] - 48);
 
    // Initialize cnt as number of ones
    // in string S
    int cnt = pref[n - 1];
 
    // Store the required result
    int ans = cnt;
 
    // Traverse the string, S
    for (int i = 0; i < n - K; i++) {
 
        // Store the number of 1s in the
        // substring S[i, i+K-1]
        int value = pref[i + K - 1] - (i - 1 >= 0 ? pref[i - 1] : 0);
 
        // Update the answer
        ans = Math.Min(ans, cnt - value + (K - value));
    }
 
    // Return the result
    return ans;
}
 
// Driver Code
public static void Main()
{
    // Given Input
    string s = "101";
    int K = 2;
 
    // Function Call
     Console.Write(minimumHammingDistance(s, K));
    }
}
 
// This code is contributed by SURENDRA_GANGWAR.


Javascript


输出
2

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