File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes general question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "general question" Watch "general question" New topic

general question

Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
i got an assignment, to write an efficient program that would select the longest substring of the same character from a string.
let's say character is 'a' and string is aasddaaa. So the resulting output is "aaa".
And here comes the question: how should i understand word efficient, is there an algorithm or smth. I would be very appreciated if someone claryfied it to me.
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
Efficient to me would mean code that runs most efficiently. There is very little overhead, no unnecessary objects instantiated, none left hanging around when not necessary.
From the sounds of your question though, I'd say you have to come up with the cleverest way of performing this with the least amount of written code.
Jeremy Thornton
Ranch Hand

Joined: Feb 21, 2002
Posts: 91
I'd say that the aim of the assignment is to get you to think about problem solving a little rather than just writing code.
For this kind of problem, there are a range of "right" solutions but I'm guessing that whoever set the assignment just wants you to have a go at implementing what you perceive is a reasonably effective solution.
e.g. one way to solve it would be to step through the alphabet and count all the instances of each letter in the String... this would obviously work for strings using only the letters of the english language, but would require lots of unnecessary comparisons.
better to start with the first letter, count the instances...
For this kind of assignment, by far the best solution is to have a go, get something to work, analyse where code is redundant or calculations are lengthy and try to work on that part in isolation.
Have fun!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Use Java 1.4 regular expressions!

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Preetham M

Joined: Sep 18, 2002
Posts: 12
You could use the Dynamic Programming Algoritham(LCS, Longest Common Sequence) to calculate the longest sub-string of a string.
Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
thank you for your replies
I agree. Here's the link:
subject: general question
It's not a secret anymore!