
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Flip All 0s in Given Binary Strings K Times with Different Neighbours
The task of flipping 0s in a binary string while considering their adjacent characters has practical applications in various domains. In this tutorial, we delve into the problem of modifying a given binary string by repeatedly flipping the 0s that have different adjacent characters. Specifically, we aim to solve this problem within the context of C++ programming.
The solution involves iteratively scanning the string and applying the necessary flips based on the provided logic. By leveraging the string manipulation capabilities, we can efficiently transform the binary string by flipping 0s K times, ensuring that each flip adheres to the criterion of having different neighbours. Through an insightful examination of the problem statement, accompanied by a step-by-step implementation in C, C++, Java and Python this tutorial provides a comprehensive guide to tackling this intriguing binary string manipulation challenge. So let's get started!
Problem Statement
Given a binary string and the number of flips, the task is to flip all occurrences of '0' in the string while considering neighbouring characters. The objective is to change '0' to '1' and, if the neighbouring characters are '1', flip them to '0' as well.
Sample Example 1
Input
binary string: "01001"; Number of flips: 4
Output
Flipped string: 00011
Explanation: The initial binary string is "01001". After applying the flips, all '0's are converted to '1's. The neighbouring characters are also flipped if they are '1'. The resulting flipped string is "00011".
Sample Example 2
Input
binary string: 101010; Number of flips: 2
Output
Flipped string: 010110
Explanation In this case, the initial binary string is "101010". Since there are no '0's in the string, no flips can be applied. Therefore, the resulting flipped string remains the same as the input string, which is "010110".
Algorithm
Start by defining the flipZeroes function that takes the binary string and the number of flips as input.
Create a copy of the binary string and store it in the result variable.
Determine the length of the binary string and store it in the variable n.
Iterate through the string using a loop, starting from index 0, until either the end of the string is reached or the number of flips becomes 0.
Check if the current character is '0'.
If it is, flip the '0' to '1' by assigning '1' to the current index in the result string.
Check the neighbouring characters (if any) and flip them if they are '1'.
Decrease the number of remaining flips by 1.
Return the resulting flipped string.
In the main function, provide an example binary string and the number of flips.
Call the flipZeroes to function with the binary string and flips as arguments.
Output the original binary string, the number of flips, and the resulting flipped string.
Overall, the program allows flipping '0's in the binary string, considering neighbouring characters and displays the output with the original string, the number of flips, and the resulting flipped string.
Example
Implementation of the above Algorithm in various programming languages
The below programs takes a binary string as input and performs a specified number of flips on the string. The flipZeroes function iterates through the string, flipping '0's to '1's and checking neighbouring characters to flip them if possible. The flips variable determines the maximum number of flips to be performed. The program then outputs the original binary string, the number of flips, and the resulting flipped string.
Note The program assumes that the input string contains only 0s and 1s. If the input string contains other characters, the program may not work as expected.
#include <stdio.h> #include <string.h> #include <stdlib.h> // Function to flip '0's in a binary string char* flipZeroes(const char* binaryString, int flips) { char* result = strdup(binaryString); int n = strlen(binaryString); // Loop through the string for (int i = 0; i < n && flips > 0; ++i) { // Check if the current character is '0' if (result[i] == '0') { // Flip the '0' to '1' result[i] = '1'; // Check the neighbors and flip them if possible if (i - 1 >= 0 && result[i - 1] == '1') { result[i - 1] = '0'; } else if (i + 1 < n && result[i + 1] == '1') { result[i + 1] = '0'; } --flips; // Decrease the number of remaining flips } } return result; } int main() { const char* binaryString = "01001"; int flips = 4; printf("Input binary string: %s\n", binaryString); printf("Number of flips: %d\n", flips); char* result = flipZeroes(binaryString, flips); printf("Flipped string: %s\n", result); free(result); // Don't forget to free the allocated memory return 0; }
Output
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
#include <iostream> #include <string> std::string flipZeroes(const std::string& binaryString, int flips) { std::string result = binaryString; int n = binaryString.length(); // Loop through the string for (int i = 0; i < n && flips > 0; ++i) { // Verify whether the current character is equal to '0' if (result[i] == '0') { // Change the current '0' to '1' by flipping it result[i] = '1'; // Check the neighbours and flip them if possible if (i - 1 >= 0 && result[i - 1] == '1') { result[i - 1] = '0'; } else if (i + 1 < n && result[i + 1] == '1') { result[i + 1] = '0'; } --flips; // Decrease the number of remaining flips } } return result; } int main() { std::string binaryString = "01001"; int flips = 4; std::cout << "Input binary string: " << binaryString << std::endl; std::cout << "Number of flips: " << flips << std::endl; std::string result = flipZeroes(binaryString, flips); std::cout << "Flipped string: " << result << std::endl; return 0; }
Output
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
public class BinaryStringFlip { // Function to flip '0's in a binary string public static String flipZeroes(String binaryString, int flips) { char[] result = binaryString.toCharArray(); int n = binaryString.length(); int i = 0; while (i < n && flips > 0) { if (result[i] == '0') { result[i] = '1'; if (i - 1 >= 0 && result[i - 1] == '1') { result[i - 1] = '0'; } else if (i + 1 < n && result[i + 1] == '1') { result[i + 1] = '0'; } flips--; } i++; } return new String(result); } public static void main(String[] args) { String binaryString = "01001"; int flips = 4; System.out.println("Input binary string: " + binaryString); System.out.println("Number of flips: " + flips); String result = flipZeroes(binaryString, flips); System.out.println("Flipped string: " + result); } }
Output
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
def flip_zeroes(binary_string, flips): result = list(binary_string) n = len(binary_string) i = 0 while i < n and flips > 0: if result[i] == '0': result[i] = '1' if i - 1 >= 0 and result[i - 1] == '1': result[i - 1] = '0' elif i + 1 < n and result[i + 1] == '1': result[i + 1] = '0' flips -= 1 i += 1 return ''.join(result) binary_string = "01001" flips = 4 print("Input binary string:", binary_string) print("Number of flips:", flips) result = flip_zeroes(binary_string, flips) print("Flipped string:", result)
Output
Input binary string: 01001 Number of flips: 4 Flipped string: 00011
Note The output may be different for different input strings and values of K.
Conclusion
To sum up, the problem of flipping 0s in a binary string K times with different neighbours can be effectively solved using the power of string manipulation. By following the provided logic and iterating through the string, we can identify the 0s that need to be flipped based on their adjacent characters. The implementation discussed in this tutorial demonstrates a clear and concise approach to addressing this problem, offering a practical solution that can be applied to real-world scenarios. Whether it is data processing, algorithmic problem-solving, or other related tasks, the ability to efficiently modify binary strings by flipping 0s with different neighbours is a valuable skill. By understanding the problem statement and leveraging the capabilities of various programming languages, readers can confidently tackle similar challenges and expand their programming expertise.