• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

Need help to remove duplicate from a string using recursion

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Can you have a sensible result from toString() on a Stream? A Stream is an Object, so you can use the un‑overridden version of toString, but how are you going to print its contents? A Stream does not “have” any contents...


Sure it does; it has elements. I just don't care how, where or when they're produced, providing they ARE produced when I use it.

And I say that an IntStream produced by String.chars() (which I - and I suspect Pawel - think should have been a CharStream) has very specific contents, which we are likely to want to convert back to a String at some point; so why not provide us with a simple way of doing it rather than having to faff about with that 'orrible collect() function.

Winston
 
Marshal
Posts: 78657
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:. . . Sure it does; it has elements. I just don't care how, where or when they're produced, providing they ARE produced when I use it. . . .

They are not so much produced as come from somewhere else, e.g. a List, or the preceding Stream. They don't so much exist in the Stream as run along it, so how are you going to catch them?

Yes, a CharSequence#charStream method would probably have been useful. And a CharStream class. But if it is so easy to write
mapToObj(i -> Character.valueOf((char)i)
I don't think it is going to happen.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, a CharSequence#charStream method would probably have been useful. And a CharStream class. But if it is so easy to write
mapToObj(i -> Character.valueOf((char)i)
I don't think it is going to happen.


Ooof. To get a Stream<Character> which is even more difficult to convert back to a String (or a char[])?

The fact is - as we tell beginners often enough - that characters are NOT numbers, even though char is a numeric type, so the likelihood of needing to do any number crunching with an IntStream returned by String.chars() seems very remote.

What doesn't seem remote to me is that I might want to do all kinds of filtering or replacement on such a stream - including stuff like using regexes - but all these tools are designed to work with chars, not ints. Furthermore, I'm likely to want to convert the result back (or into) to a String, or even an OutputStream.

It's not particularly the business of carrying the value of a char in an int that I object to; it's that no provision appears to have been made for the fact that people might want to manipulate streams of characters.

Winston
 
Campbell Ritchie
Marshal
Posts: 78657
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right, Winston, that this sort of code makes s.replaceAll("[aeiouAEIUO]", "") look really beautiful. I couldn't get such code to compile without the .map() call.
 
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
i solved this problem this way



this is my first participation , hope it was useful
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

What happens if you try this in your main method?

Some other thoughts: what happens when you make two calls to clean_string in main?

The Java convention for naming with multiple "words" is ThisIsAClassName and thisIsAMethodName. That is, camel case with classes starting with an uppercase letter and methods starting with a lowercase letter, no underscores.
 
Yousef Salah
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:Welcome to the Ranch!

What happens if you try this in your main method?

Some other thoughts: what happens when you make two calls to clean_string in main?

The Java convention for naming with multiple "words" is ThisIsAClassName and thisIsAMethodName. That is, camel case with classes starting with an uppercase letter and methods starting with a lowercase letter, no underscores.



thanks for your corrections , i highly appreciate them

i fix the issues you mentioned except the case if i call cleanString multiple time because of the static string field ,
i tried to solve this issue by trying to copy the string object with "new" and return new string
but when clear the temp string it clears the new string object too
here is my code to clarify

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general -- and especially when you are a beginning programmer -- the use of a static variable is a sign that something is wrong with the design. Can you think of a way to not use a static variable?
 
Yousef Salah
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:In general -- and especially when you are a beginning programmer -- the use of a static variable is a sign that something is wrong with the design. Can you think of a way to not use a static variable?



yes , i just migrated from php +2 years exp. with strong understanding of oop , so do you have any advices for me about that ?
regarding the problem i removed the static variable but the problem is the same

variables in java have big difference from php

 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code is better -- more OOP -- but what about not using any static or instance variable? What if you worked on the passed string itself?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Javco Aract for the contribution. I have hidden your post because in this forum we don't like to post complete solutions, but instead provide help to the OP so that they can discover their own solution.

Edit: I unhide the post because we have had another solution visible for 13 hours.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Check this solution out. My test cases all passed. The key point is that even though left and righ are "cleaned" the concatenation of them may be not. All other conditions are base cases.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Normally we don't provide complete solutions, but this solution has been visible for 13 hours so far, so I think we can compare solutions now.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ranajoy Saha wrote:Hi, Everyone

Here's the question

Given a string, return recursively a "cleaned" string where adjacent chars that are the same have been reduced to a single char. So "yyzzza" yields "yza".

stringClean("yyzzza") → "yza"
stringClean("abbbcdd") → "abcd"
stringClean("Hello") → "Helo"

The iterative approach would be this.



The signature that I must stick to is public String stringClean(String str) and have to solve it without using replace() or replaceWith(). How should I approach writing my program now?

Regards,
Ranajoy Saha





A very nice and challenging question.
 
I don't always make ads but when I do they're tiny
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic