给定一个字符串,将其组成的所有可能长度的字符串都列出来

2023年 8月 29日 21.8k 0

给定一个字符串,将其组成的所有可能长度的字符串都列出来

在本节中,我们将看到如何生成任意长度的所有可能字符串,这将采用每个字符的组合来生成字符串。例如,如果字符串是ABC,则它将生成 - {A,B,C,AB,BA,BC,CB,CA,AC,ABC,ACB,BAC,BCA,CAB,CBA}

让我们看一个例子来理解。

算法

printAllString(str)

Begin
n := length of the string str
count is 2^n – 1
for each number 0 to count, do
sub_str := empty string
for j in range 0 to n, do
if jth bit of the counter is set, then
concatenate jth character of str with sub_str
end if
done
repeat:
print sub_string
until next permutation of sub_string is not completed
done
End

登录后复制

示例

#include
#include
#include
using namespace std;
void printAllString(string str) {
int n = str.size();
unsigned int count = pow(2, n);
for (int counter = 1; counter

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论