I have a string "ECET" and I would like to create all the possible strings where I substitute one or more letters (all but the first) with "X".
So in this case my result would be:
> result
[1] "EXET" "ECXT" "ECEX" "EXXT" "EXEX" "ECXX" "EXXX"
Any ideas as to how to approach the issue?
This is not just create the possible combinations/permutations of "X" but also how to combine them with the existing string.
charAt(0); String st=s. substring(1); Set<String> qq=find(st); for(String str:qq) { for(int i=0;i<=str. length();i++) { ss. add(comb(str,c,i)); } } } return ss; } public static String comb(String s,char c,int i) { String start=s.
The number of possible combinations that are possible with 26 letters, with no repetition, is 67,108,863.
There are 325 possible combinations with two letters.
Following are the several approaches to generate all the combinations of a string in JavaScript- In this approach we will use the data structure called an array and will run two for loops on the given string which is actually the main logical part of our code Further we will use .push () and .slice () method to add our result into an array.
Combined sets can have a prefix and/or suffix added via the prefix/suffix fields. Delimit objects within each set via the delimit field. Join sets via join field.
Sets can be joined with any text but enter \x for new line. Test by clicking the "Generate Combinations" button above to watch the default objects combine. WARNING: 10 objects in combinations of 6 will produce 1000000 combinations! Many browsers will display a stop script message or lock-up.
Object Input Box - Enter objects to combine with each on a new line. Generate objects into combinations of which will produce sets. Repeat objects: yes no.
Using the FUN
argument of combn
:
a <- "ECET"
fun <- function(n, string) {
combn(nchar(string), n, function(x) {
s <- strsplit(string, '')[[1]]
s[x] <- 'X'
paste(s, collapse = '')
} )
}
lapply(seq_len(nchar(a)), fun, string = a)
[[1]] [1] "XCET" "EXET" "ECXT" "ECEX" [[2]] [1] "XXET" "XCXT" "XCEX" "EXXT" "EXEX" "ECXX" [[3]] [1] "XXXT" "XXEX" "XCXX" "EXXX" [[4]] [1] "XXXX"
unlist
to get a single vector. Faster solutions are probably available.
To leave your first character unchanged:
paste0(
substring(a, 1, 1),
unlist(lapply(seq_len(nchar(a) - 1), fun, string = substring(a, 2)))
)
[1] "EXET" "ECXT" "ECEX" "EXXT" "EXEX" "ECXX" "EXXX"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With