0

for below mentioned string permutation algo or any other recursive algo, what is max size of string supported if i have 1 GB of dedicated memory available.

public void permutate(String prefix, String word){

    if(word.length() <= 1){
        System.out.println(prefix + word);
    } else{
        for (int i = 0; i < word.length(); i++) {
            String temp = word.substring(0,i) + word.substring(i+1);
            permutate(prefix + word.charAt(i), temp);
        }
    }
}

public void permutate(String word){
    permutate("", word);
}
banjara
  • 3,800
  • 3
  • 38
  • 61

1 Answers1

0

Well your memory complexity O(N^2) (N being the length of the word) so for 1 GB I would say you could do well with a word of about 16000 characters (16000^2 * 4 bytes equals roughly 1 GB).

However from time point of view a word of about 100 characters could take days to compute, and a word of 1000 chars could take months even years I think. In any case if you run this algorithm for a word with 16000 characters you'll probably never get to see the end of it.

Tiberiu Savin
  • 221
  • 2
  • 10
  • 2
    You're off by several orders of magnitude: when `O(N!)` algorithms are considered, the "magic number" is 12: you can fit `12!` primitive operations in one second. `20!` lasts for about a year; `22!` takes 400 years. A step of this algorithm takes some 50..100 primitive operations plus an I/O operation that is 1000 times slower. Therefore a string of length 15 would keep an average computer busy for a year or so; a 16-character string will keep it busy until it's ready to be thrown away. – Sergey Kalinichenko Jun 02 '12 at 11:32