2
$\begingroup$

Problem Description : I need to create all substrings from a string by deleting letters from it without re-arranging the rest of the letters. For example, "us" is a substring of "substring", but "gs" is not . Duplicates should be counted. For example, "peep" has two substrings "pep", corresponding to the two choices of "e". The code below I ran does not give me desired output.

subString[s_String] := StringCases[s, {_, __}, Overlaps -> All]

I would appreciate if anyone can help.

$\endgroup$
2
  • 2
    $\begingroup$ subString[s_String] := Map[StringJoin, Subsets[Characters[s]]] $\endgroup$ Jun 10 at 15:42
  • $\begingroup$ I think you are correct, I added remove the empty element in the list in code below. Thank you very much. $\endgroup$ Jun 11 at 14:39
1
$\begingroup$

I tested the below code is correct, contributed from user3257842

DeleteCases[Map[StringJoin, Subsets[Characters[s]]], ""]

The code above will give us the non-empty sublists of a string by deleting characters from it.

Thanks.

$\endgroup$
1
  • $\begingroup$ The null string always appears first in the list so Drop[Map[StringJoin, Subsets[Characters[s]]], 1] would also work. $\endgroup$ Jun 12 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.