Ranajoy Saha wrote:Given a string, return recursively a "cleaned" string where adjacent chars that are the same have been reduced to a single char.
You need to think about more cases. Your iterative solution doesn't look right, while it was a nice try.
Ranajoy Saha wrote:The iterative approach would be this.
It doesn't seem you're returning something.
Ranajoy Saha wrote:Given a string, return ... a "cleaned" string...
That was one of the problems I saw.
Liutauras Vilda wrote:. . . It doesn't seem you're returning something. . . .
Ranajoy Saha wrote:f("ab...") = "a" + f("b..."), is it?
Ranajoy Saha wrote:But how to reduce f("aab...") = f("ab..."), is my problem.
Zachary Griggs wrote:Another slightly more unusual way you could do it is to create a Set of type character, then add every character from the string into the Set, then combine it back into a String. The point being, Sets do not allow duplicates. Perhaps not best performance-wise though.
Ranajoy Saha wrote:But how to reduce f("aab...") = f("ab..."), is my problem. I have one algorithm in mind, as we are using recursion, we need to start from back, so I shall check if someString.lenth()-1 == someString.length()-2 and if so, then f(someString(0,someString.length()-1)).
Ranajoy Saha wrote:Solved it without using temp variable or a separate method...
Ranajoy Saha wrote:I don't recall what Junlu's test's were. So, what are they?
Knute Snortum wrote:Is the "e" variable just so you don't calculate the length of "s" every time?
Piet Souris wrote:these "utility" methods are ugly!
Liutauras Vilda wrote:However, I'd go with slightly different approach.
Piet Souris wrote:yes, this is what I had in mind (actually, the slightly easier "front" variant:
Paweł Baczyński wrote:Which is why one can assume the problem is clearly theoretical which is why the theoretical solution is prefect as it is clear and short.
Winston Gutkowski wrote:Classic case of the "theoretical/real-life" divide; and there is no "right" answer.
Winston Gutkowski wrote:
Ranajoy Saha wrote:BTW, ("".equals(s) || ...) is a safer way to check than what you wrote. The expression (str.equals("") || ...) will still fail with a NullPointerException if str is null.
I did not understand this part. Why is it so?
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
a bit of art, as a gift, the permaculture playing cardshttps://gardener-gift.com