We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
When you use a TreeSet you actually do a lot of unnecessary work: you sort each word to put it in the right place in the tree. When you look at the API, adding operation for a TreeSet is O(log(n)). So the running time of this algorithm will be O(n)* O(log(n).
O(log(n) is just this unnecessary sorting (testing each word against O(log(n) words), O(n) is getting all the possible 3-letter strings.
Now, when you don't use a tree you only do 2 operations: test each word against the current minimum and maximum word, which is a constant operation O(1). So the running time of this algorithm is O(n)* O(1) = O(n).
O(1) is 2 tests on each word, O(n) is getting all the possible 3-letter strings.
To sum up, the TreeSet needs significantly more time to get the solution - maybe it doesn't matter for 2000 words but for a book, it will.
Correct me if I'm wrong, as I don't do coding professionally.
Java Substring Comparisons
You are viewing a single comment's thread. Return to all comments →
I'm afraid, beauty is only skeep deep. :-)
When you use a TreeSet you actually do a lot of unnecessary work: you sort each word to put it in the right place in the tree. When you look at the API, adding operation for a TreeSet is O(log(n)). So the running time of this algorithm will be O(n)* O(log(n).
O(log(n) is just this unnecessary sorting (testing each word against O(log(n) words), O(n) is getting all the possible 3-letter strings.
Now, when you don't use a tree you only do 2 operations: test each word against the current minimum and maximum word, which is a constant operation O(1). So the running time of this algorithm is O(n)* O(1) = O(n). O(1) is 2 tests on each word, O(n) is getting all the possible 3-letter strings.
To sum up, the TreeSet needs significantly more time to get the solution - maybe it doesn't matter for 2000 words but for a book, it will.
Correct me if I'm wrong, as I don't do coding professionally.