Question

Write a function that determines if a string has the same number of 0’s and 1’s...

Write a function that determines if a string has the same number of 0’s and 1’s using a stack. The function must run in O(N) time. You can assume there already exists a stack class and can just use it

(Java Please)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 

Explanation::

Code in JAVA is given below

Please read comments for better understanding of the code

NOTE: Assumption is made that string consist only of 1's and 0's

Code in JAVA::

import java.util.Scanner;

import java.util.Stack;

public class StackCounter {

public static void main(String[] args) {

/**

* Using Scanner class object named sc to take input from the user

* */

Scanner sc = new Scanner(System.in);

/**

* An String variable named binary is declared below

* */

String binary;

/**

* Below we ask user to enter the binary String

* */

System.out.print("Enter binary string (1's and 0's) : ");

binary = sc.nextLine();

/**

* An integer variable named halfLength is declared below

* It stores the division of length of string binary by 2

* */

int halfLength = binary.length()/2;

/**

* If binary is odd length string then no need to use stack at all

* */

if(binary.length()%2!=0){

System.out.println("No equal number of 1's and 0's. ODD LENGTH STRING!");

}else{

/**

* An Stack of type Character named binaryStack is declared below

* We will populate in it only 1's

* */

Stack<Character> binaryStack = new Stack<Character>();

/**

* Using for loop we will traverse through the stack

* and perform push for only '1'

* */

char ch;

for(int i = 0; i < binary.length(); i++){

/**

* we store current character at index i in binary string

* into ch variable

* */

ch = binary.charAt(i);

if(ch=='1'){

/**

* If ch is 1 then push into stack

* */

binaryStack.push(ch);

}

}

/**

* Now if size of the stackBinary and half length of string binary is same

* then there are equal number of 1's and 0's

* */

if(halfLength == binaryStack.size()){

System.out.println("Equal number of 1's and 0's!!!");

}else{

System.out.println("No equal number of 1's and 0's.");

}

}

}

}

OUTPUT::

Test Case 1:

Enter binary string (1's and 0's) : 101

No equal number of 1's and 0's. ODD LENGTH STRING!

Test Case 2:

Enter binary string (1's and 0's) : 1010

Equal number of 1's and 0's!!!

Test Case 3:

Enter binary string (1's and 0's) : 1000111100

Equal number of 1's and 0's!!!

Please provide feedback!!

Thank You!!

Add a comment
Know the answer?
Add Answer to:
Write a function that determines if a string has the same number of 0’s and 1’s...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program using java that determines whether an input string is a palindrome; that is,...

    Write a program using java that determines whether an input string is a palindrome; that is, whether it can be read the same way forward and backward. At each point, you can read only one character of the input string; do not use an array to first store this string and then analyze it (except, possibly, in a stack implementation). Consider using multiple stacks. please type out the code :)

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines...

    Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly braces { }, and brackets [].  For example, the string {a(b+ac)d} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac)d} do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Write...

  • Question 4 3+1+2-6pts) Write a java program to find the minimum number of an inte same...

    Question 4 3+1+2-6pts) Write a java program to find the minimum number of an inte same contents after the methods is executed. nteger stack. The stack should keep the Use the fol lowing method declaration for writing your java code a stack of type integer and return the minimum number in it Params: A stack of integers Return: minimum number t findMin(Stackkints S) t is the time and space complexity of your code for a Stack of size n elements?

  • C++ 1) Write a function named WordCount, which determines the number of words in a “C...

    C++ 1) Write a function named WordCount, which determines the number of words in a “C type” string. The function will have one parameter (the address of the string) and will return the number of words (words are separated by one or more spaces). (15 points) 2) Write a function named VowelConsonant which will have three parameters - all “C type” strings. The function will place all vowels (the characters a, e, i, o, u) from the first string into...

  • Write a program in C++ that reads a string consisting of a positive integer or a...

    Write a program in C++ that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. DO NOT USE STACK LIBRARY OR STACK FUNCTIONS OR A STRING LIBRARY. For this assignment you have to use STACK with all methods to convert the decimal number to numeric...

  • Write a function count_vowels(s) that takes a string as an argument and returns the number of...

    Write a function count_vowels(s) that takes a string as an argument and returns the number of vowels ('a', 'e', 'i' 'o', 'u') in the string. Should you use a for or while loop? (Implement this as a function, not as a class with a method.) Be sure to include unittest test cases to demonstrate that your code works properly, e.g Part 2: last_occurance(target, sequence) Write a function last_occurance(target, sequence) that takes two arguments: 1. target: A target item to find...

  • Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces...

    Write a client function parenthesesMatch that given a string containing only the characters for parentheses, braces or curly braces, i.e., the characters in ’([{}])’, returns True if the parentheses, brackets and braces match and False otherwise. Your solution must use a Stack. For, example: >>> parenthesesMatch('(){}[]') True >>> parenthesesMatch('{[()]}') True >>> parenthesesMatch('((())){[()]}') True >>> parenthesesMatch('(}') False >>> parenthesesMatch('({])') False >>> parenthesesMatch('((())') False >>> parenthesesMatch('(()))') False >>> Hint: It is not sufficient to just count the number of opening and closing...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • Write a function vowelCount(S) that takes a string, S, and returns a new dictionary containing entries...

    Write a function vowelCount(S) that takes a string, S, and returns a new dictionary containing entries of the form v:N where v is one of ’aeiou’ and N is the number of occurrances of v in S.Your dictionary should never contain any entries where N is 0; thus, for example, if the S is "wipeout", the dictionary returned would be {′e′:1,′i′:1,′o′:1,′u′:1 }and not {′a′:0,′e′:1,′i′:1,′o′:1,′u′:1}. NOTE: must use a comprehension

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT