algorithm - Printing lexicographically sorted subsets from a given string -
for ex - if string "abc", answer should ab abc ac b bc c (only lexicographically smallest combination set of characters should appear) have solved problem string containing 15 or more characters, taking lot of time. how can reduce running time of algorithm? here , n length of string. here code:
int n = int.parse(console.readline()); var str = console.readline(); string coll = string.empty; coll = coll + " " + str[0]; (int j = 1; j < n; j++) { var items = coll.split(' '); foreach (var item in items) { coll = coll + " " + item+str[j]; } } var tt = coll.split(' ').orderby(a => a); foreach (var item in tt) if (!string.isnullorempty(item)) console.writeline(item);
for string of length n, there 2^n possible subsets. if each of subsets needs printed, can't around exponential complexity.
Comments
Post a Comment