📌  相关文章
📜  查找最小序列翻转使所有位相同的位

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

查找最小序列翻转使所有位相同的位

给定一个仅由 1 和 0 组成的二进制字符串。找到最小连续序列翻转次数可以使字符串的所有位相同的位(输出为 1 或 0)。
这里,连续序列翻转意味着翻转子串或 0 或 1。例如,在字符串“00011110001110”中,我们可以在一次翻转中将字符串更改为“11111110001110”。前三个连续零更改为 1。
注意

  • 我们可以一次性更改此字符串的任何连续序列。
  • 如果 0 和 1 都可能,则打印最后出现的那个。
  • 任务是简单地打印最小序列翻转将使所有位相同的位 1 或 0。

例子

朴素的方法:开始遍历字符串并使用名为gropupOfOnes和另一个groupOfZeros的变量来计算 0 和 1 的组。现在,比较 1 组和 0 组的计数。计数较少的将是答案。
如果两者相等,则答案将是字符串的最后一个字符。
时间复杂度:O(N)
有效的方法

  • 仔细观察字符串,字符串最后索引处的字符在字符串中会有更多组(如果字符串的第一个和最后一个字符相等)。所以,答案将是另一个字符。
  • 如果第一个字符和最后一个字符不相等,则两个字符具有相同的组数。因此,答案将是最后一个索引处的字符。

下面是上述方法的实现:

C++
// C++ program to find which bit sequence
// to be flipped
 
#include 
using namespace std;
 
// Function to check which bit is to be flipped
char bitToBeFlipped(string s)
{
    // variable to store first and
    // last character of string
    char last = s[s.length() - 1];
    char first = s[0];
 
    // Check if first and last characters
    // are equal, if yes, then return
    // the character which is not at last
    if (last == first) {
        if (last == '0') {
            return '1';
        }
        else {
            return '0';
        }
    }
 
    // else return last
    else if (last != first) {
        return last;
    }
}
 
// Driver Code
int main()
{
    string s = "1101011000";
 
    cout << bitToBeFlipped(s) << endl;
 
    return 0;
}


Java
// Java program to find which bit sequence
// to be flipped
 
class GfG {
 
// Function to check which bit is to be flipped
static char bitToBeFlipped(String s)
{
    // variable to store first and
    // last character of string
    char last = s.charAt(s.length() - 1);
    char first = s.charAt(0);
 
    // Check if first and last characters
    // are equal, if yes, then return
    // the character which is not at last
    if (last == first) {
        if (last == '0') {
            return '1';
        }
        else {
            return '0';
        }
    }
 
    // else return last
    else if (last != first) {
        return last;
    }
    return last;
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "1101011000";
 
    System.out.println(bitToBeFlipped(s));
}
}


Python 3
# Python 3 program to find which bit
# sequence to be flipped
 
# Function to check which bit is
# to be flipped
def bitToBeFlipped( s):
 
    # variable to store first and
    # last character of string
    last = s[len(s) - 1]
    first = s[0]
 
    # Check if first and last characters
    # are equal, if yes, then return
    # the character which is not at last
    if (last == first) :
        if (last == '0') :
            return '1'
     
        else :
            return '0'
 
    # else return last
    elif (last != first) :
        return last
 
# Driver Code
if __name__ == "__main__":
     
    s = "1101011000"
 
    print(bitToBeFlipped(s))
 
# This code is contributed by ita_c


C#
// C# program to find which bit sequence
// to be flipped
using System;
 
class GfG {
 
// Function to check which bit is to be flipped
static char bitToBeFlipped(String s)
{
    // variable to store first and
    // last character of string
    char last = s[s.Length - 1];
    char first = s[0];
 
    // Check if first and last characters
    // are equal, if yes, then return
    // the character which is not at last
    if (last == first) {
        if (last == '0') {
            return '1';
        }
        else {
            return '0';
        }
    }
 
    // else return last
    else if (last != first) {
        return last;
    }
    return last;
}
 
// Driver Code
public static void Main()
{
    string s = "1101011000";
 
    Console.WriteLine(bitToBeFlipped(s));
}
}
// This code is contributed by anuj_67..


PHP


Javascript


输出:
0

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