c# - Find all combinations in a string separated -
i'm trying combinations in string in c# idea in mind:
given string foo
want list<string>
values:
f o o fo o foo f oo
as can see it's not easy substring chars in string separated spaces.
i've tried doing like:
list<string> result = new list<string>(); string text = "foo"; (int = 1; < foo.lenght; i++) { //i'm stucked --> think stupid , don't know how procede or not fast enough. i'm stuck. }
edit: there correct answers it's clear of them won't since strings working have between 55 , 85 chars each 1 means best function in answers give me between 2^54 , 2^84 possible combinations , bit much.
it clear find combinations , afterwards them won't do. i'll have drop it.
first things first: if string length n, 2^n strings output. so, if want treat strings of length 70, have problem.
you can use counter, enumerating 0 2^n, , treat bitwise mask: if first bit 1, put space between first , second char, if it's zero, don't.
so, unsigned long of length 64 barely enough treat strings of length 65.
an example implementation, without recursion (it's more verbose other examples), should lot faster other implementations long inputs:
public ienumerable<string> getpartitionedstrings(string s) { if (s == null) yield break; if (s == "") { yield return ""; yield break; } if (s.length > 63) throw new argumentoutofrangeexception("string long..."); var arr = s.tochararray(); for(ulong = 0, maxi = 1ul << (s.length - 1); < maxi; i++) { yield return putspaces(arr, i); } } public string putspaces(char[] arr, ulong spacespositions) { var sb = new stringbuilder(arr.length * 2); sb.append(arr[0]); ulong l = 1; (int = 1; < arr.length; i++, l <<= 1) { if ((spacespositions & l) != 0ul) sb.append(" "); sb.append(arr[i]); } return sb.tostring(); }
probably away bit field, in billions of strings, try reformulate bit problem.