import java.util.ArrayList; import java.util.List; /** * BracketExpension */ public class BracketExpension { // You are given a string expression which consists of several comma separated // tokens // enclosed within opening ('{') and closing ('}') curly braces. // The string expression might or might not have a prefix before opening curly // brace('{') and // a suffix after closing curly brace ('}'). // You have to return a list of strings as output for each comma separated item // as shown below in the examples. // // Example 1: // Input = "/2022/{jan,feb,march}/report" // Output = "/2022/jan/report" // "/2022/feb/report" // "/2022/march/report" // // Example 2: // Input = "over{crowd,eager,bold,fond}ness" // Output = "overcrowdness" // "overeagerness" // "overboldness" // "overfondness" // // Example 3: // Input = "read.txt{,.bak}" // Output = "read.txt" // "read.txt.bak" public static void main(String[] args) { String tokens = "over{crowd,eager,bold,fond}ness"; String tokeneez = "/2022/{jan,feb,march}/report"; List options = TokensListed(tokeneez); for (int i = 0; i < options.size(); i++) { System.out.println(options.get(i)); } } public static List TokensListed(String str) { List ans = new ArrayList<>(); // let's find the starting and the ending of the indecies of the curly braces; int startIndex = str.indexOf("{"); int endIndex = str.indexOf("}"); if (startIndex == -1 || endIndex == -1 || startIndex > endIndex) { ans.add(str); return ans; } // Extract the prefix, suffix and the options; String prefix = str.substring(0, startIndex); String suffix = str.substring(endIndex + 1); String options = str.substring(startIndex + 1, endIndex); // Split the options by comma String[] tokens = options.split(","); for (String token : tokens) { ans.add(prefix + token + suffix); } return ans; } }