简体   繁体   中英

java string permutations

I'm writing an android app where you have a recursive function that takes a string and returns all the permutations of that string and all it's substrings. This approach was time consuming especially with longer strings. I went on this site and asked if there was a more efficient way of permuting a string and a couple of people suggested a Trie tree. Surely enough the trie was way faster but what I've also noticed that the Trie performance improves with longer strings. For example, using a 7 char long string the Trie is about 2.5 times faster. A 10 char string the Trie is about 5 times faster and 12 char string the Trie is about 10 times faster. Does anyone know why the Trie's performance gets better with longer strings?

It's not that the trie's performance improves with longer strings - it of course becomes slower. The point is that your naive solution is worse than the trie solution, so it becomes slower at a faster rate .

If you were to compare the trie solution to a more efficient algorithm (if one exists) then you would see the opposite effect.

You could instead measure the wall time performance of each algorithm to see at what rate each algorithm gets slower. Or you could analyze the algorithms themselves and try to express their performance in big-O notation which would allow you to more easily compare them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM