#finding shortest length of a String by means of an Array list

1 messages · Page 1 of 1 (latest)

keen orchid
vocal thicketBOT
#

<@&987246399047479336> please have a look, thanks.

keen orchid
#

Im not sure how to do it properly

severe merlin
#

the approach should be as follows:

#

take the current length
compare it to the currently best known length
if shorter, update that
repeat the loop

#

at the end of the loop, the memorized value is the shortest

#

ur doing too much in ur loop

#

dont compare to the next value

#

compare the current to the memorized instead

#

example, "aaa", "b", "cc", u start with the first element, aaa. length is 3

#

u memorize 3

#

next iteration

#

b, length 1. thats better than 3. update

#

next iteration

#

cc, length 2. thats not better than 1. continue

#

end of loop

#

memorized: 1

keen orchid
#

so how many variable should I have....2?

a temp and the answer?

severe merlin
#

before u deal with the answer, for now only try to find the shortest length

#

we can do the answer in the next step

keen orchid
#

so something like this?

severe merlin
#

u kinda did more now, but okay

#

did u try it out? or are u only starring at it

#

u should try it out

#

overall it looks correct. with the exception of the starting value for shortestLength

#

which shouldnt be 0

#

it must be the "worst initial guess"

#

which is Integer.MAX_VALUE

#

otherwise no item will be "shorter" and overwrite it

keen orchid
#

but is this from the mathsclass?

cauuse im not allowed to use it

severe merlin
#

no its not

#

the only other alternative is to use the first item as initial guess and adjust ur logic accordingly