Solving Beautiful Binary String with 1 Variable

Photo by Hope House Press - Leather Diary Studio on Unsplash

Out of Free Stories? Here is my Friend Link.

I have published an ebook. A compilation of 100 Java(Interview) Programming problems which have been solved . I have given clear explanation and the code in the book. Believe me when I say, this will kick start you to achieve the job at your dream company.

Click on this link to get you to the landing page. It is completely free when you use kindle amazon. Take a look at it.

Beautiful Binary String

Alice has a binary string. She thinks a binary string is beautiful if and only if it doesn’t contain the substring “010”.

In one step, Alice can change a 0 to 1 a or vice versa. Count and print the minimum number of steps needed to make Alice see the string as beautiful.

Input Format

The first line contains an integer n, the length of binary string.
The second line contains a single binary string b.

Output Format

Print the minimum number of steps needed to make the string beautiful.

Code

Explanation

The minimum number of changes required to convert a “010" into a beautiful string is by changing the middle number to 0. Thus 1 change is enough for changing a 010 to beautiful string.

So our job is to find how many 010 sub strings are present in the string. Then print the value of the number of substrings.

Number of sub strings= minimum number of steps needed to make the string beautiful

Algorithm

  • Read the value of the string B.
  • Initialize the value of count to be zero.
  • Traverse through the array, And check if the value of three adjacent elements will be equal to “010”.
  • If the value is equivalent to 010. Increase the value of count.
  • At the end of the traversal, Print the value of count.

Working of the Code

7
0101010

Initial value:

B[]= “0101010”;

count=0;

Traversal through the String

i=0;

B[0]==0; ? true;

B[1]==1; ? true;

B[2]==0; ? true;

count++;

count =1;

i+=2;

i=2;

i++;

B[3]==0; ? false;

continue;

B[4]==0?; true;

B[5]==1?; true;

B[6]==0?; true

count++;

count=2

End of the traversal.

Print the value of count.

Result : count =2

Conclusion

Don’t forget to hit the follow button✅to receive updates when we post new coding challenges. Tell us how you solved this problem in the comments section below. 🔥 We would be thrilled to read them. ❤

I have published an ebook. A compilation of 100 Java(Interview) Programming problems which have been solved . (HackerRank) 🐱‍💻

Click HERE 🧨🎊🎃

This is completely free 🆓 if you have amazon kindle subscription.

Code -> Understand-> Repeat is my motto. I am a Data Engineer who writes about everything related to Data Science and Interview Preparation for SDE.

Recommended from Medium

How Feedzai ARMS automates rule management in large scale systems

So in love with coding: 2 Kotlin Coroutines Articles & Kobby Plugin Article & Funny Programming 💘

7 Best Professional Scrum Master (PSM) Certification Training Courses and Practice Tests in 2021

Becoming a Self-Taught Programmer: Cory Althoff Interview

The missing CI/CD Kubernetes component: Helm package manager

Alan’s solution to sharing data knowledge

How to use Kali Linux for beginners | part2

Easiest Way to OCR Process PDF Documents in ASP.NET Core

We can easily perform OCR process in PDF documents with the Syncfusion OCR Processor Library in .NET Core platform.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
House of Codes

House of Codes

Code -> Understand-> Repeat is my motto. I am a Data Engineer who writes about everything related to Data Science and Interview Preparation for SDE.

More from Medium

Ultimate Leetcode Challenge — Blind List

A SHED UNDER THE TREE DATA STRUCTURE

39. Combination Sum

Post Optimize Bubble Sort