문제

Given a string: "ABCD", return the substrings with one or more missing characters keeping the order of the strings. This doesn't seem to be a "permutation", but I'm not sure if this algorithm has a name. I am using this to generate anagrams of a word and words within a word.

Example:

A B C D

AB BC CD AC AD << missing BC

ABC BCD ACD ABD

도움이 되었습니까?

해결책

You're generating the ordered power set of the characters of the input string - meaning, all the subsets you can get from the set of characters in the input string, preserving the initial order:

input  = { A, B, C, D }
output = { {}, {A}, {B}, {C}, {D}, {A, B}, {B, C} {C, D}, {A, C}, {A, D},
           {B, D}, {A, B, C}, {A, B, D}, {A, C, D}, {B, C, D}, {A, B, C, D} }

The resulting set has 2^n elements (with n being the size of the input set), you might want to remove the empty set and the input set from the result, but basically this is the algorithm you're looking for. It's easy to find implementations for any language you want.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top