📌  相关文章
📜  最小化给定二进制字符串中的位翻转以使 10 的计数等于 01

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

最小化给定二进制字符串中的位翻转以使 10 的计数等于 01

给定二进制字符串str ,任务是选择任何索引并将其更改为01 ,并以最小的步骤执行此操作,以使子字符串01的计数等于10

例子:

方法:如果我们可以观察到如果字符串的第一个和最后一个字符相同,那么“01”的计数等于1 ,因为根据归纳,字符串中间总是存在一个字符,所以我们可以拆分一个字符串分成两部分s[1….i], [i…n] 所以 AB(s) = BA(s)

  • 如果第一个字符和最后一个字符不同,则将第一个字符更改为最后一个字符。
  • 执行上述步骤后,打印str的值作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to make the count equal
string MakeEqual(string str)
{
 
    // Take first and last char of string
    char FirstChar = str[0];
    char LastChar = str[str.size() - 1];
 
    // Compare both the char
    if (FirstChar != LastChar) {
 
        // Copy lastchar inplace of
        // firstchar or viceversa
        str[0] = LastChar;
    }
 
    // If above condition is not true so
    // string remain unchanged
    // Return string
    return str;
}
 
// Driver Code
int main()
{
 
    string str = "0110101";
    string ans = MakeEqual(str);
    cout << ans;
    return 0;
}


Java
// Java program for the above approach
class GFG
{
   
  // Function to make the count equal
  static String MakeEqual(String str) {
 
    // Take first and last char of String
    char FirstChar = str.charAt(0);
    char LastChar = str.charAt(str.length() - 1);
 
    // Compare both the char
    if (FirstChar != LastChar) {
 
      // Copy lastchar inplace of
      // firstchar or viceversa
      str = str.substring(1, str.length());
      str = LastChar + str;
 
    }
 
    // If above condition is not true so
    // String remain unchanged
    // Return String
    return str;
  }
 
  // Driver Code
  public static void main(String args[]) {
 
    String str = "0110101";
    String ans = MakeEqual(str);
    System.out.println(ans);
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# python3 program for the above approach
 
# Function to make the count equal
def MakeEqual(str):
 
    # Take first and last char of string
    FirstChar = str[0]
    LastChar = str[-1]
 
    # Compare both the char
    if (FirstChar != LastChar):
 
        # Copy lastchar inplace of
        # firstchar or viceversa
        str[0] = LastChar
 
    # If above condition is not true so
    # string remain unchanged
    # Return string
    return ''.join(str)
 
# Driver Code
if __name__ == "__main__":
 
    str = "0110101"
    ans = MakeEqual(list(str))
    print(ans)
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
 
public class GFG
{
   
  // Function to make the count equal
  static String MakeEqual(String str) {
 
    // Take first and last char of String
    char FirstChar = str[0];
    char LastChar = str[str.Length - 1];
 
    // Compare both the char
    if (FirstChar != LastChar) {
 
      // Copy lastchar inplace of
      // firstchar or viceversa
      str = str.Substring(1, str.Length - 1);
      str = LastChar + str;
 
    }
 
    // If above condition is not true so
    // String remain unchanged
    // Return String
    return str;
  }
 
  // Driver Code
  public static void Main(String []args) {
 
    String str = "0110101";
    String ans = MakeEqual(str);
    Console.WriteLine(ans);
  }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出:
1110101

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