Мы можем генерировать все подстроки строки, используя концепцию битов, которая была упомянута ранее.Вот код (на C ++, но у вас есть идея) для этого: -
string s;
int n = s.size();
int num = 1<<n;
for(int i =1; i< num ; i++){ //Checks all the permutations.
int value = i;
int j, pos;
for (j=1, pos=1; j < num; j<<=1, pos++) //Finds the bits that are set
if (i & j)
cout<<s[pos-1]; //You can print s[n-pos] to print according to bit position
cout<<endl;
}
Например; - Строка s = abc ,
The size is 3 . So we check from 1 to 7 ( 1<<3).
for i = 1 ( 001 ) , the first bit is set, so a is printed.
for i = 2 ( 010 ) , the second bit is set, so b is printed.
for i = 3 ( 011 ) , the first and second bit are set, so ab is printed.
.
.
.
for i = 7 ( 111 ) , all three bits are set, so abc is printed.