Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom String converter.

 
Raghu Devatha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was doing an exercise and wanted to know if my code is efficient.

Input="aaabbaccaab"
Expected Output=a3b2a1c2a2b1


I hope there is a better program.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15715
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your program looks OK. Are you unhappy with it, and is there a specific thing in the code that you are unhappy with? Can you explain what that is?

One small change would be to use StringBuilder instead of StringBuffer - StringBuilder is a newer version of StringBuffer that doesn't have unnecessary synchronization. You will not notice any speed difference from making that change though in such a small and simple program.

In line 16, you could write counter++; instead of counter = counter + 1; but that doesn't make the code any more efficient.
 
Raghu Devatha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper.

When I showed it to my instructor his first impression was I was using too many variables (increasing memory footprint). I was trying to reduce the number of variables used without loosing the readability.

Thanks again for the information on StringBuilder.



 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use toCharArray() and tempOut, use String's own length() and charAt(int) methods. That way, you don't need to copy the entire String's contents into a new char[]. Because that's what toCharArray() does.

Now, about the number of variables:
- input: you'll need it. That's the String to convert.
- store: you'll need it. How else can you determine when to start a new counter?
- tempOut: you can get rid of this because of my above statement.
- i: you'll need it. It's the loop counter.
- counter: you'll need it. How else can you determine how many times a character occurred?
- result: you'll need it.

So apart from tempOut you can't get rid of any of the variables.
 
Raghu Devatha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Rob for you expert opinion. It really helps.
 
Rob Spoor
Sheriff
Posts: 20822
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your information, as a little exercise for myself I rewrote your code using a nested loop. Unless I do not cache c (my version of "store") I still have the same number of variables at 5.
In this code, "c" is the equivalent of "store" and your "counter" is equivalent to "j - i". My code only has one advantage over yours - it can handle empty Strings. For the rest there isn't much difference.
 
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!