给定长度为N的二进制字符串S ,任务是通过最少的操作次数,从长度仅为N的N组成的字符串(例如T )中获取S。每个操作都涉及从字符串S中选择任何索引i并翻转字符串T的索引[i,N – 1]上的所有位。
例子:
Input: S = “101”
Output: 3
Explanation:
“000” -> “111” -> “100” -> “101”.
Therefore, the minimum number of steps required is 3.
Input: S = “10111”
Output: 3
Explanation:
“00000” -> “11111” -> “10000” -> “10111”.
Therefore, the minimum number of steps required is 3.
方法:
这样做是为了找到1中第一次出现在给定的字符串S和该索引处执行给定的操作。此步骤之后,对于在特定索引处S和T的字符的每个不匹配项,请重复该操作。
请按照以下步骤操作:
- 遍历S并标记1的第一次出现。
- 初始化两个变量,例如last和ans ,其中last存储最后执行操作的字符( 0或1 ), ans保留所需的最小步数的计数。
- 从第一个出现的1迭代到字符串。
- 如果当前字符为0并且last = 1 ,则增加ans的计数,并设置last = 0 。
- 否则,如果last = 0且当前字符为1 ,则设置last = 1 。
- 返回ans的最终值。
下面是上述方法的实现:
C++
// C++ Program to implement
// the above approach
#include
using namespace std;
// Function to find the minimum
// number of operations required
// to obtain the string s
int minOperations(string s)
{
int n = s.size();
int pos = -1;
// Iterate the string s
for (int i = 0; i < s.size(); i++) {
// If first occurrence of 1
// is found
if (s[i] == '1') {
// Mark the index
pos = i;
break;
}
}
// Base case: If no 1 occurred
if (pos == -1) {
// No operations required
return 0;
}
// Stores the character
// for which last operation
// was performed
int last = 1;
// Stores minimum number
// of operations
int ans = 1;
// Iterate from pos to n
for (int i = pos + 1; i < n; i++) {
// Check if s[i] is 0
if (s[i] == '0') {
// Check if last operation was
// performed because of 1
if (last == 1) {
ans++;
// Set last to 0
last = 0;
}
}
else {
if (last == 0) {
// Check if last operation was
// performed because of 0
ans++;
// Set last to 1
last = 1;
}
}
}
// Return the answer
return ans;
}
// Driver Code
int main()
{
string s = "10111";
cout << minOperations(s);
return 0;
}
Java
// Java program to implement the
// above approach
import java.util.*;
class GFG{
// Function to find the minimum
// number of operations required
// to obtain the string s
static int minOperations(String s)
{
int n = s.length();
int pos = -1;
// Iterate the string s
for(int i = 0; i < s.length(); i++)
{
// If first occurrence of 1
// is found
if (s.charAt(i) == '1')
{
// Mark the index
pos = i;
break;
}
}
// Base case: If no 1 occurred
if (pos == -1)
{
// No operations required
return 0;
}
// Stores the character
// for which last operation
// was performed
int last = 1;
// Stores minimum number
// of operations
int ans = 1;
// Iterate from pos to n
for(int i = pos + 1; i < n; i++)
{
// Check if s[i] is 0
if (s.charAt(i) == '0')
{
// Check if last operation was
// performed because of 1
if (last == 1)
{
ans++;
// Set last to 0
last = 0;
}
}
else
{
if (last == 0)
{
// Check if last operation was
// performed because of 0
ans++;
// Set last to 1
last = 1;
}
}
}
// Return the answer
return ans;
}
// Driver code
public static void main(String[] args)
{
String s = "10111";
System.out.println(minOperations(s));
}
}
// This code is contributed by offbeat
Python3
# Python3 program to implement
# the above approach
# Function to find the minimum
# number of operations required
# to obtain the string s
def minOperations(s):
n = len(s)
pos = -1
# Iterate the string s
for i in range(len(s)):
# If first occurrence of 1
# is found
if (s[i] == '1'):
# Mark the index
pos = i
break
# Base case: If no 1 occurred
if (pos == -1):
# No operations required
return 0
# Stores the character
# for which last operation
# was performed
last = 1
# Stores minimum number
# of operations
ans = 1
# Iterate from pos to n
for i in range(pos + 1, n):
# Check if s[i] is 0
if (s[i] == '0'):
# Check if last operation was
# performed because of 1
if (last == 1):
ans += 1
# Set last to 0
last = 0
else:
if (last == 0):
# Check if last operation was
# performed because of 0
ans += 1
# Set last to 1
last = 1
# Return the answer
return ans
# Driver Code
if __name__ == "__main__":
s = "10111"
print(minOperations(s))
# This code is contributed by chitranayal
C#
// C# program to implement the
// above approach
using System;
class GFG{
// Function to find the minimum
// number of operations required
// to obtain the string s
static int minOperations(String s)
{
int n = s.Length;
int pos = -1;
// Iterate the string s
for(int i = 0; i < s.Length; i++)
{
// If first occurrence of 1
// is found
if (s[i] == '1')
{
// Mark the index
pos = i;
break;
}
}
// Base case: If no 1 occurred
if (pos == -1)
{
// No operations required
return 0;
}
// Stores the character
// for which last operation
// was performed
int last = 1;
// Stores minimum number
// of operations
int ans = 1;
// Iterate from pos to n
for(int i = pos + 1; i < n; i++)
{
// Check if s[i] is 0
if (s[i] == '0')
{
// Check if last operation was
// performed because of 1
if (last == 1)
{
ans++;
// Set last to 0
last = 0;
}
}
else
{
if (last == 0)
{
// Check if last operation was
// performed because of 0
ans++;
// Set last to 1
last = 1;
}
}
}
// Return the answer
return ans;
}
// Driver code
public static void Main(string[] args)
{
String s = "10111";
Console.Write(minOperations(s));
}
}
// This code is contributed by rock_cool
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)