I have a collection of strings, I need to create a regex pattern to filter out the strings that has duplicate character only appear twice.
Eg: Arrays.asList("abcdef","bababc","abbcde","abcccd","aabcdd","abcdee","ababab");
Here , I want to end up in a result of ["bababc","abbcde","aabcdd","abcdee"]
So the duplicate character can be consecutive character or intermediate character .But duplication of a character twice is given precedence over any other duplication count
Eg:"bababc" , where 'a' is repeated twice and 'b' is repeated three times , since 'a' is repeated twice it get eligible for the filtering.
I tried with different patterns mentioned
- here this works partially only in case of intermediate character, but takes string without duplicates also
- A variation of this here , this works partially with consecutive chars after sort the string
Can some one help me ?