Beautiful Binary String in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank, Algorithm Solutions of Problem Solving Section in Java. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Beautiful Binary String in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Introduction To Algorithm

The word Algorithm means “a process or set of rules to be followed in calculations or other problem-solving operations”. Therefore Algorithm refers to a set of rules/instructions that step-by-step define how a work is to be executed upon in order to get the expected results. 

Advantages of Algorithms:

  • It is easy to understand.
  • Algorithm is a step-wise representation of a solution to a given problem.
  • In Algorithm the problem is broken down into smaller pieces or steps hence, it is easier for the programmer to convert it into an actual program.

Link for the ProblemBeautiful Binary String– Hacker Rank Solution

Beautiful Binary String – Hacker Rank Solution

Problem:

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

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

Example

She can change any one element and have a beautiful string.

Function Description

Complete the beautifulBinaryString function in the editor below.

beautifulBinaryString has the following parameter(s):

  • string b: a string of binary digits

Returns

  • int: the minimum moves required

Input Format

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

Constraints

  • .

Output Format

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

Sample Input 0

STDIN       Function
-----       --------
7           length of string n = 7
0101010     b = '0101010'

Sample Output 0

2  

Explanation 0:

In this sample, 

The figure below shows a way to get rid of each instance of :

1462078460 7a0995ab28 binary

Make the string beautiful by changing  characters ( and ).

Sample Input 1

5
01100

Sample Output 1

0

Sample Case 1:

In this sample 

Explanation 1

The substring  does not occur in , so the string is already beautiful in  moves.

Sample Input 2

10
0100101010

Sample Output 2

3

Explanation 2

In this sample 

One solution is to change the values of  to form a beautiful string.

Beautiful Binary String– Hacker Rank Solution
import java.util.Scanner;

/**
 * @author Techno-RJ
 *
 */
public class BeautifulBinaryString {
	 static int minSteps(int n, String strB){
	        int stepsCount=0;
	        String tmpStr=strB;
	        while(tmpStr.indexOf("010")!=-1){
	        	tmpStr=tmpStr.replaceFirst("010", "011");
	        	stepsCount++;
	        }
	        return stepsCount;
	    }

	    public static void main(String[] args) {
	        Scanner in = new Scanner(System.in);
	        int n = in.nextInt();
	        String B = in.next();
	        int result = minSteps(n, B);
	        System.out.println(result);
	        in.close();
	    }
	}

103 thoughts on “Beautiful Binary String in Algorithm | HackerRank Programming Solutions | HackerRank Problem Solving Solutions in Java [💯Correct]”

  1. whoah this blog is wonderful i really like reading your articles. Keep up the great paintings! You realize, a lot of people are hunting round for this info, you could help them greatly.

    Reply
  2. whoah this blog is wonderful i really like reading your articles. Keep up the great paintings! You realize, a lot of people are hunting round for this info, you could help them greatly.

    Reply
  3. I am overwhelmed by your post with such a nice topic. Usually I visit your blogs and get updated through the information you include but today’s blog would be the most appreciable. Well done!

    Reply
  4. Al-Ahliyya Amman University
    AAU Started providing academic services in 1990, Al-Ahliyya Amman University (AAU) was the first private university and pioneer of private education in Jordan. AAU has been accorded institutional and programmatic accreditation. It is a member of the International Association of Universities, Federation of the Universities of the Islamic World, Union of Arab Universities and Association of Arab Private Institutions of Higher Education. AAU always seeks distinction by upgrading learning outcomes through the adoption of methods and strategies that depend on a system of quality control and effective follow-up at all its faculties, departments, centers and administrative units. The overall aim is to become a flagship university not only at the Hashemite Kingdom of Jordan level but also at the Arab World level. In this vein, AAU has adopted Information Technology as an essential ingredient in its activities, especially e-learning, and it has incorporated it in its educational processes in all fields of specialization to become the first such university to do so.

    https://www.ammanu.edu.jo/

    Reply

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock