• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question: Remove Zeroes (need a java approach)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are given a string s. Each character is either 0 or 1.

You want all 1s in the string to form a contiguous subsegment. For example, if the string is 0, 1, 00111 or 01111100, then all ones (1) form a contiguous subsegment, and if the string is 0101, 100001 or 11111111111101, then this condition is not met.

You may erase some (possibly none) zeroes(0) from the string. What is the minimum number of zeros (0) that you have to erase?
Input
The first line contains one integer t (1≤t≤100) — the number of test cases.

Then t lines follow, each representing a test case. Each line contains one string s (1≤|s|≤100); each character of s is either 0 or 1.

Output
Print t integers, where the i-th integer is the answer to the i-th testcase (the minimum number of zeros(0) that you have to erase from s).

Example
Input


Output


Note
In the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111).
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Bhakti,

welcome to the Ranch and enjoy the stay!

Do you have any question about this exercise?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I shall give you a hint: Strings are immutable objects, but they have a mutable counterpart, called StringBuilder.
Another hint: The question doesn't ask you to convert the String, “0010101101001010101011010101011100110100,” to, “001111111111111111111100.” It asks for a count of 0s.
What does the following mean?

s (1≤|s|≤100)

Neither the absolute value operator |x| nor inequalitycan be applied to text.
There is no absolute value operator in Java®.
 
Bhakti Khedkar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its the number of characters we can input,ie ,the maximum length of string.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case it should read s (1≤|s.length()|≤100).
There should be no need for a limit to the number of Strings nor their individual length; a correctly written program should be able to handle any number. The absolute value operator is redundant since Strings never have a negative length.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done getting a solution, but please don't edit old posts. That can cause confusion and can also cause you solution to be ignored. I am refusing the edit; please post the solution in a new post. Please use the code button; I shall use it on your original post so you can see how much better it looks
 
Bhakti Khedkar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK SIR.....I wont edit the original post.
Also, sir I am new to coderanch . Hence, trying to get familiar with it.
 
Bhakti Khedkar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for showing us your solution; it works nicely (), butI think I can improve it. I would create a class to do the calculations, which I shall show you last.
I am not convinced that for loops are the best loops for all cases. You can change the loop in line 21 to a while loop:-You can do the same with the loop in line 20, but that may be slightly easier.
There is a very naughty way to reduce those loops to a single line each
Please reserve // comments for commenting‑out and short things; if you need to write anything long use /* comments */ instead.
Don't declare that method throws Exception because it doesn't throw any checked exceptions.
Don't declare import java.lang.Anything; because that is always unnecessary.
Don't declare import java.io.Anything; because you are using nothing from that package. Only declare import java.util.Scanner; because that is the only import you are using.
Make sure your variable names are obvious; I can't tell what pt1 and pt2 mean from simply reading them.
Make sure to indent your code, which you have done quite well except for lines 11 and 41, but you are mixing K&R and Allman indentation. Use one convention and stick to it throughout.
Make sure to space your code out with spaces around binary operators.Regard the following as the standard version of the for loop. It uses < and 0 rather than <= and 1:-The bottom quote shows a suggestion of how ZeroCounter can be used. It may be possible to “lose” the local variables in lines 18‑19.

JShell on my computer wrote:|  created class ZeroCounter
. . .
jshell> new ZeroCounter("0010101101001010101011010101011100110100").countIncluded0s()
$4 ==> 16

 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[edit]There is a mistake in this post, corrected later.
Can I do it with a Stream? See Java™ Tutorials (parts of each section in that link), Stream interface, and java.util.stream package.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid I missed out a line from the Stream code. Sorry. Corrected version follows, with line 3 added.
 
Bhakti Khedkar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you sir for giving me optimized solution for the question.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a way without using Streams:
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This morning, I wrote:. . . I am not convinced that for loops are the best loops for all cases. . . .

When I changed it to a while loop, I was using sentinel‑controlled repetition; a for loop works best with counter‑controlled repetition.
 
reply
    Bookmark Topic Watch Topic
  • New Topic