我有一句话是这样的:
fath
输出单词中所有字母的组合,例如:
f
fa
fat
fath
ft
fth
fh
答案1
编辑:
这是一个单行awk
解决方案:
echo f a t h | awk '{for(i=0;i<2^NF;i++) { for(j=0;j<NF;j++) {if(and(i,(2^j))) printf "%s",$(j+1)} print ""}}'
输出
f
a
fa
t
ft
at
fat
h
fh
ah
fah
th
fth
ath
fath
(此幂集实现的修改版本https://stackoverflow.com/questions/40966428/awk-power-set-implementation)
答案2
重击:
combos () {
local word=$1
local len=${#word}
local first=${word:0:1}
echo "$first"
for ((i=1; i < len; i++ )); do
for ((j=1; i+j <= len; j++ )); do
echo "$first${word:i:j}"
done
done
}
然后
$ combos fath
f
fa
fat
fath
ft
fth
fh