DEV Community

Discussion on: Algorithms Problem Solving: Jewels and Stones

Collapse
arcticspacefox profile image
ArcticSpaceFox

Hmm weird, during testing of both algorithms they take the same amount of time, do you see why that is? I test them on 10 Million random chars of aAbB

Collapse
teekay profile image
TK Author

I think it is because you are running with short length strings (aAbB). The first solution will be very slow for the worst-case scenario, where the string has a long length.

Imagine this example: strings with 10,000 chars.

The first solution has a nested for. The combination of each character would be 10,000 * 10,000 = 100,000,000.

The second solution has two for, but not nested. It would be 10,000 + 10,000 = 20,000 for the worst-case scenario.

The curve increases differently for each solution. Did it make sense to you?

Just for curiosity: how are you testing the amount of time spent by each algorithm?

Collapse
arcticspacefox profile image
ArcticSpaceFox

I use timeit and measure time before and after. Thank you I will try to modify my file which is tested on