Skip to content

Instantly share code, notes, and snippets.

@smaugho
Created April 27, 2018 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smaugho/f18040e9422587082db5dacccf2e7777 to your computer and use it in GitHub Desktop.
Save smaugho/f18040e9422587082db5dacccf2e7777 to your computer and use it in GitHub Desktop.
public class RunningClass {
public static void main(String [] args)
{
List<String> list = new LinkedList<>();
process(Collections.singletonList("2890"), list);
for (String element : list) {
System.out.println(element);
}
}
public static void process(List<String> parts, List<String> list) {
for (int partIndex = 0; partIndex < parts.size(); partIndex++) {
String part = parts.get(partIndex);
List<String> previousPart = partIndex > 0? parts.subList(0, partIndex) : null;
List<String> lastParts = partIndex < parts.size() - 1? parts.subList(partIndex + 1, parts.size()) : null;
for (int i = 1; i < part.length(); i++) {
List newParts = new LinkedList();
if (previousPart != null) newParts.addAll(previousPart);
newParts.add(part.substring(0, i));
newParts.add(part.substring(i));
if (lastParts != null) newParts.addAll(lastParts);
process(newParts, list);
}
}
String result = "";
for (String part : parts) {
result = result + part + " ";
}
result = result.trim();
if (!list.contains(result)) {
list.add(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment