我正在尝试在 Matlab 中编写伪代码和算法,以返回字符串的所有子序列。
所以字符串X = {ABCD}
会返回XSubSequence = {A, B, C, D, AB, AC, AD, BC, BD, CD, ABC, ABD, BCD, ABCD}
,顺序当然没关系。
clear
x = 'ABC';
XSize = length(x);
count = 1;
i=1;
for i=1:XSize
ZSubSequence{count} = x(i);
count = count + 1;
for j=i+1:XSize
temp = strcat(x(i),x(j));
ZSubSequence{count} = temp;
count = count + 1;
for k=i+2:XSize
if j ~= k
temp = strcat(x(i), x(j), x(k));
ZSubSequence{count} = temp;
count = count + 1;
end
end
end
end
有什么办法可以让这更加动态,所以我可以添加X
任何大小并且它能够处理它?