[Solved] Generating logic for possible output of all the combinations of string in a list [closed]


If all you have to do is to out put true or false depending on the user input, you can convert your code strings to regular expressions and check if input matches the list of regex.

Steps:

  • Convert each element in your codes list to a regex

    // convert “ABC(Q,E,1)EEE” to “ABC[QE1]EEE” to match each string starting with ABC followed by one of [QE1] and ending with EEE

    //”R(1,2,3,4,5)RT(U,M,N,B,V,H)(Q,E,R,F,G,H)(R,Z)” to “R[12345]RT[UMNBVH][QERFGH][RZ]”

    etc

  • Check if input matches one of the regexes

Example:

public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the code you want to check: ");
    String input = scan.next();

    List<String> codes = new ArrayList<>();
    codes.add("ABC(Q,E,1)EEE");
    codes.add("ABDCE(E,Z,X)E");
    codes.add("B(A,1)AAEEE");
    codes.add("R(1,2,3,4,5)RT(U,M,N,B,V,H)(Q,E,R,F,G,H)(R,Z)");
    codes.add("B(A,1)AA(E,Z)EE");

    //list to store the modified strings
    List<String> modifiedCodes = new ArrayList<>();
    //for each string in list find if there is a pattern like '('some chars')'
    Pattern p = Pattern.compile("\\(.*\\)");
    for (Iterator<String> i = codes.iterator(); i.hasNext();) {
        String code = i.next();
        StringBuffer  sb = new StringBuffer ();
        Matcher m = p.matcher(code);
         while (m.find()) { 
            String match = m.group();
            //if found a match replace '(' and ')' with '[' and ']' and remove commas
            m.appendReplacement(sb, match.replace('(', '[').replace(')', ']').replace(",", ""));
          }
          m.appendTail(sb);
          //add modified string to list
          modifiedCodes.add(sb.toString());
    }     

    boolean codeIsPresent = false;
    for(String code: modifiedCodes){
        //check if input matches one of the regex in the list 'modifiedCodes'
        if (input.matches(code)) {
            codeIsPresent = true;
            System.out.println("True: This code is present");
            break;
        }
    }
    if(!codeIsPresent){
        System.out.println("Code not found");
    }
}

EDIT

how can we print the list of all the combinations of the string from
which it is getting the output? say, I just have a string
“BA(1,2,3)QW(A-Z,0-9)” and I want all the possible combinations of it

The above question from your coments is slightly difference as the original post, it might be better if you post a new question. You can create your own algorithm with somekind of tree structure to solve the issue but it can be very hackish and messy. I would suggest to use a 3rd party libraray like generex if possible. You can download the jar from the maven repo here. With generex you can have all your possible commbinations:

public static void main(String args[]){
    //change your input to a regular expression
    //"BA(1,2,3)QW(A-Z,0-9)"  to  "BA[1-3]QW[A-Z][0-9]"
    Generex generex = new Generex("BA[1-3]QW[A-Z][0-9]");
    List<String> matchedStrs = generex.getAllMatchedStrings();
    matchedStrs.forEach(System.out::println);
} 

7

solved Generating logic for possible output of all the combinations of string in a list [closed]