একটি স্ট্রিং থেকে যেকোনো দৈর্ঘ্যের সব সম্ভাব্য স্ট্রিং জেনারেট করা (সব কবিনেশন এবং পারমুটেশন)। উদাহরণ: "ABC" → {A,B,C,AB,BA,BC,CB,CA,AC,ABC,ACB,BAC,BCA,CAB,CBA}
অ্যালগরিদম
printAllString(str)
Begin n := length of str count := 2^n - 1 for counter = 1 to count do sub_str := "" for j = 0 to n-1 do if jth bit of counter set then concatenate str[j] to sub_str end if done repeat print sub_str until next_permutation done done End
উদাহরণ
#include <iostream> #include <algorithm> #include <cmath> using namespace std; void printAllString(string str) { int n = str.size(); unsigned int count = pow(2, n); for (int counter = 1; counter < count; counter++) { string subs = ""; for (int j = 0; j < n; j++) { if (counter & (1<<j)) subs.push_back(str[j]); } do { cout << subs << endl; } while (next_permutation(subs.begin(), subs.end())); } }আউটপুট
A B AB BA C AC CA BC CB ABC ACB BAC BCA CAB CBA D AD DA ... (all permutations)