0

How could I split a string into chunks of all available combinations? For example:

"12345"

Would output:

[1, 12, 123, 1234, 12345, 2, 23, 234, 2345, 3, 34, 345, 4, 45]

This is as far as I've gotten:

String title = "12345";
List<String> keywordsList = List();
String temp = "";
String temp2 = "";
for (int i = 0; i < title.length; i++) {
  temp = temp + title[i];
  if (temp.length > 1) temp2 = temp2 + title[i];
  keywordsList.add(temp);
  if (temp2.length != 0) keywordsList.add(temp2);
 }
 print(keywordsList);
 return keywordsList;

},

Which results in:

[1, 12, 2, 123, 23, 1234, 234, 12345, 2345]

Super stuck now, will appreciate any help.

Thanks in advance!

Mr Jax
  • 957
  • 12
  • 22
  • i don't think you are stuck on language syntax, you need a logical implemantation which is provided [here](https://stackoverflow.com/questions/4240080/generating-all-permutations-of-a-given-string). don't really know if this counts as a duplicate question. – Mücahid Erenler May 15 '20 at 11:19

1 Answers1

1

You can achieve in following way.

String number = '12345';
List<String> listnumber = number.split("");
List<int> output = [];
for (int i = 0; i < listnumber.length; i++) {
  if (i != listnumber.length - 1) {
    output.add(int.parse(listnumber[i]));
  }
  List<String> temp = [listnumber[i]];
  for (int j = i + 1; j < listnumber.length; j++) {
    temp.add(listnumber[j]);
    output.add(int.parse(temp.join()));
  }
}
print(output.toString());
Viren V Varasadiya
  • 25,492
  • 9
  • 45
  • 61