Regex - find anagrams and subanagrams

I have a character pool and I want to combine all the words that are anagrams of these characters or a subset of these characters using a regular expression.

Example: given the string "ACNE" the regular expression should give me the following results:

  • ACNE [T]
  • CENA [T]
  • CAN [T]
  • CAAN [F]
  • CANEN [F]

I tried this solution /b[acne]{1,4}/b, but it accepts multiple repetitions of single characters. What can I do to take char no more than once each time?

+5
source share
2 answers

Subanagrams of the word acne are words that

  • consist of letters only acne
  • do not contain amore than once
  • do not contain cmore than once
  • do not contain nmore than once
  • do not contain emore than once

Compiling this into a regex:

^(?!.*a.*a)(?!.*c.*c)(?!.*n.*n)(?!.*e.*e)[acne]*$

: regexpal

, "" , "" - ,

  • acne
  • .

:

^(?!.*(.).*\1)[acne]*$

: regexpal

: ""

^(?!.*([agoid]).*\1)(?!(.*m){3})[magoid]*$

( agoid <2 → 2 > )

+9

Java, DataStructure, Algorithms Interview Questions. ,

https://github.com/arpans2112/techsqually-java8-best-practices/blob/master/src/com/techsqually/java/library/util/regularexpression/anagramStrings.java

package com.techsqually.java.library.util.regularexpression;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class anagramStrings {


    public static void main(String[] args) {

       int count = findAnagramsInAGivenStrings("arpan","Hi arpan Aarpn we are testing rapan rranp anagram");
        System.out.println(count);
    }


    /**
     * <p> Use to find the number of anagrams of a word in a Given String</p>
     * @param : word : is the word for which you want to find the anagrams
     * @param : givenString : is the string in which you want to find the anagrams of word given
     * @return : total number of anagrams of the word passed
     *  
     *  all words in which each character count is same but their order can be different 
     *  e.g arpan and rapan are anagrams 
     *  
     * @output of above given example is 3, "arpan" , "Aarpn" and rapan are anagrams of arpan
     * */
    public static int findAnagramsInAGivenStrings(String word, String givenString){

        word = word.toLowerCase();
        givenString = givenString.toLowerCase();
        HashMap<String,Integer> numberOfAnnagrams = new HashMap<>();
       Matcher matcher = Pattern.compile("[" + word + "]{" + word.length() + "}").matcher(givenString);

       int count = 0;
        while (matcher.find()){

                 char[] matchWordArray = matcher.group().toCharArray();
                 char[] givenWordArray = word.toCharArray();
            Arrays.sort(matchWordArray);
            Arrays.sort(givenWordArray);

            if (Arrays.equals(matchWordArray,givenWordArray)) count++;
        }

        return count;
    }
}
0

All Articles