E
E
eywsf2021-07-19 06:23:08
Java
eywsf, 2021-07-19 06:23:08

How to get all one and two letter combinations in order from a word?

For example, there is a line You need to get all possible combinations, such as:

String str = "program";


  • program
  • progr-am
  • prog-ra-m
  • pr-og-r-am


and so on.

How to do it in java? Or at least tell me the general algorithm?

Breaking the deadline is not a problem, at least one character, at least two, for example

for (int i = 0; i < str.length()-1; i = i +2){
            String strTemp = str.charAt(i)+""+str.charAt(i+1);
              System.out.println(strTemp);
}


But how to get (bypass) all possible combinations of one and two is not clear how to program.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexandroppolus, 2021-07-19
@Alexandroppolus

The simplest recurrence relation:
All layouts for the word "program" are, firstly, all layouts for "rogram" with the prefix "p-", secondly, all layouts for "gram" with the prefix "pr-". Well, special cases of a word from one letter and empty. It seems that with this approach it will be convenient to use StringBuffer

A
Antonio Solo, 2021-07-19
@solotony

p-r-o-g-r-a-m (0)

pr-o-g-r-a-m (1)
p-ro-g-r-a-m
p-r-og-r-a-m
p-r-o-gr-a-m
p-r-o-g-ra-m
p-r-o-g-r-am

pr-og-r-a-m (2)
pr-o-gr-a-m
pr-o-g-ra-m
pr-o-g-r-am
p-ro-gr-a-m
p-ro-g-ra-m
p-ro-g-r-am
p-r-og-ra-m
p-r-og-r-am
p-r-o-gr-am

pr-og-ra-m (3)
pr-og-r-am
pr-o-gr-am
p-ro-gr-am

i would do it with recursion

S
Sand, 2021-07-19
@sand3001

7 letters, if we represent this in bits as 1111111, if there is no "-" before the first letter, then we cut it off, it turns out 111111, let's say 0 is "-" before the letter, then we go through the options in a cycle from 0 to 63, for example: 110101 = pro-gr-am
000000 = program

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question