2

I'm trying to create a regex that gives me a few combination pairs from an input that I'm interested in.

If I had the following input...

"pppeeeople"

I would like to create the following permutations...

people, ppeeople, peeople, ppeople.

I can currently create 'people' and 'ppeeople' with the following two REGEXs where the input word is 'pppeeeople'.

String temp = word.replaceAll("(.)\\1{2,}", "$1$1");  //result ppeeople
String temp2 = word.replaceAll("(.)\\1{1,}", "$1");   //result people

However I would like to be able to generate a REGEX that can also grab the word with 1 set of two consective letters, such as 'ppeople', then another REGEX that can skip the first inputed double character sequence and produce 'peeople' and so forth. The basic idea is to get all permutations of the word that contain a two consecutive characters.

Is this possible with a REGEX or should I just use a loop and StringBuilder into a list?

This is close, but it produces out of bounds index errors and I would still have to remove duplicates from the ArrayList repeats I'm adding it to.

int index = temp.length();

        for (int i = 0; i < index; i++) {
            System.out.println("Index" + i + ": " + temp);
            if(temp.charAt(i) == temp.charAt(i+1)) {
                StringBuilder sb = new StringBuilder(temp);
                repeats.add(temp);
                sb.deleteCharAt(i);
                temp = sb.toString();
            }
        }
Yawn
  • 173
  • 3
  • 20

2 Answers2

0

Sorry there is no single regular expression which will return all of the permuted strings. Instead I prefer using recursive method which will return all of the strings in a list.

If you want to do so, see answers here.

In C++ there is a function in stl algorithm called next_permutation() which returns the next permuted string from the given string.

UPDATE

According to the edit of the question here is a snippet to get combinations of a given string-

public class Combination {

    //Use Set/HashSet instead of ArrayList if you do not want duplicate string
    //Set<String> combinations = new HashSet<String>();
    private ArrayList<String> combinations = new ArrayList<>();

    public void generate(String instr) {
        generate(instr, new StringBuffer(), 0);
    }
    private void generate(String instr, StringBuffer outstr, int index) {
        for (int i = index; i < instr.length(); i++) {
            outstr.append(instr.charAt(i));

            // Here you may add your rules to avoid all combinations
            combinations.add(outstr.toString());

            generate(instr, outstr, i + 1);
            outstr.deleteCharAt(outstr.length() - 1);
        }
    }

    public ArrayList<String> getCombinations() {
        return combinations;
    }
}

Here the combinations array list will contain all of the combinations as expected. You may call it like this-

Combination cmb = new Combination();

cmb.generate("pppeeeople");

ArrayList<String> list = cmb.getCombinations();

for(String str : list){
    System.out.println(str);
}

Now if you want few combinations instead of all, you should add your own rules based on length, character, repetitions etc.

Community
  • 1
  • 1
Partharaj Deb
  • 864
  • 1
  • 8
  • 22
  • Not looking for all permutations. I'm looking for a few combinations of a repeat. If you have 'hheelloo' I want to return hhelo, heelo, hello, heloo. – Yawn Feb 16 '17 at 09:31
  • But in the first line of you question you said about permutation. OK I'll update it. Now let me know, do you also want to maintain the sequence of letters from left to right? – Partharaj Deb Feb 16 '17 at 09:33
0

Not pretty but it does produce the desired output.

    int index = temp.length();

    for (int i = 0; i < index; i++) {
      //  System.out.println("Index" + i + ": " + temp);
        try{
            if(temp.charAt(i) == temp.charAt(i+1)) {
                StringBuilder sb = new StringBuilder(temp);
                repeats.add(temp);
                sb.deleteCharAt(i);
            //    System.out.println("Sb after delete: " + sb.toString());
                temp = sb.toString();
            //    System.out.println("New Temp: " + temp);
            }
        } catch (Exception e) {
            //print.stackTrace(e);
        }
    }
Yawn
  • 173
  • 3
  • 20