202k views
5 votes
Below you can see stringSize, which is implemented in Java

Below you can see stringSize, which is implemented in Java-example-1
User Vinothkr
by
5.7k points

1 Answer

6 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 Perchik
by
5.7k points