Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create all combinations of letter substitution in string

Tags:

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.

like image 242
User2321 Avatar asked Sep 06 '18 09:09

User2321


People also ask

How do you make all string combinations?

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.

How many combinations of letters are possible?

The number of possible combinations that are possible with 26 letters, with no repetition, is 67,108,863.

How many 2 letter and 2 number combinations are there?

There are 325 possible combinations with two letters.

How to generate all combinations of a string in JavaScript?

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.

How to add prefixes and suffixes to combined sets?

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.

How do I join a set of text together?

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.

How do I combine multiple objects in a set?

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.


1 Answers

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"
like image 71
Axeman Avatar answered Oct 07 '22 13:10

Axeman