505,802 views
23 votes
23 votes
Below you can see stringSize, which is implemented in Java

Below you can see stringSize, which is implemented in Java-example-1
User Aleksandar Varicak
by
2.9k points

1 Answer

27 votes
27 votes
It will return correct values as it will iterate until the length is met (and therefore i is not less than s.length() but is equal to s.length). It is poorly designed as s.length() can be used directly to obtain the number of characters in a string. This function likely has a Big O notation greater than O(n) because - due to the unnecessary loop - it has to iterate again. It also increments size multiple times instead of mutating it once when the for loop is finished to reduce on read/write time
User Michael Sparmann
by
2.5k points