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. . . .
There are three kinds of actuaries: those who can count, and those who can't.
Ranajoy Saha wrote:f("ab...") = "a" + f("b..."), is it?
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
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)).
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Ranajoy Saha wrote:Solved it without using temp variable or a separate method...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Ranajoy Saha wrote:I don't recall what Junlu's test's were. So, what are they?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
All things are lawful, but not all things are profitable.
Knute Snortum wrote:Is the "e" variable just so you don't calculate the length of "s" every time?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
There are three kinds of actuaries: those who can count, and those who can't.
Piet Souris wrote:these "utility" methods are ugly!
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Classic case of the "theoretical/real-life" divide; and there is no "right" answer.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
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?
Thinking In Java
Don't listen to Steve. Just read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|