import java.util.*;

 class StringPermutations {
    public static void main(String[] args) {
        String input = "A[20,10] | (B[20,10]|C[30,10]) - (D[30,50] | (E[40,30] - F[40,20]))";

        // Extract characters
        Set<Character> characters = new HashSet<>();
        for (char c : input.toCharArray()) {
            if (Character.isLetter(c)) {
                characters.add(c);
            }
        }

        List<Character> characterList = new ArrayList<>(characters);
        generatePermutations(characterList, new ArrayList<>(), "");
    }

    private static void generatePermutations(List<Character> characters, List<Character> permutation, String result) {
        if (permutation.size() == characters.size()) {
            System.out.println(result);
            return;
        }

        for (char c : characters) {
            if (!permutation.contains(c)) {
                List<Character> newPermutation = new ArrayList<>(permutation);
                newPermutation.add(c);

                // Create new result string
                String newResult;
                if (!result.isEmpty()) { // Check if it’s not the first character
                    // Add operators before the character if it’s not the first
                    generatePermutations(characters, newPermutation, result + " | " + c);
                    generatePermutations(characters, newPermutation, result + " - " + c);
                } else {
                    // Directly pass character if it’s the first one
                    generatePermutations(characters, newPermutation, result + c);
                }
            }
        }
    }
}
2: import java.util.*;
import java.util.concurrent.TimeUnit;

class Rectangle {
    int width;
    int height;
    String name;
    int space;

    public Rectangle(int width, int height, String name) {
        this.width = width;
        this.height = height;
        this.name = name;
        this.space = width * height;
    }
}

class RectangleGrid {
    private static List<String[][]> possiblePositioningStates = new ArrayList<>();
    private static Rectangle[] rectangles;
    private static String[][] grid;
    private static int totalArea;
    static int wSum = 0;
    static int hSum = 0;
    static List<Integer> widths = new ArrayList();
    static List<Integer> heights = new ArrayList();
    static int gridWidth, gridHeight;

    public static void main(String[] args) {
        long startTime = System.nanoTime();
        rectangles = new Rectangle[]{
                new Rectangle(2, 1, "A"),
                new Rectangle(2, 1, "B"),
                new Rectangle(3, 1, "C"),
                new Rectangle(3, 5, "D"),
                new Rectangle(4, 3, "E"),
                new Rectangle(4, 2, "F"),
                new Rectangle(7, 1, "G"),
                new Rectangle(7, 1, "H"),
                new Rectangle(1, 8, "I"),
                new Rectangle(8, 2, "J"),
//                new Rectangle(3, 2, "K"),
//                new Rectangle(5, 2, "L"),
                new Rectangle(8, 1, "M"),
        };

        Arrays.sort(rectangles, new Comparator<Rectangle>() {
            @Override
            public int compare(Rectangle r1, Rectangle r2) {
                return Integer.compare(r1.space, r2.space);
            }
        });
        int start = 0;
        int end = rectangles.length - 1;
        while (start < end) {
            Rectangle temp = rectangles[start];
            rectangles[start] = rectangles[end];
            rectangles[end] = temp;
            start++;
            end--;
        }

        for (Rectangle rectangle : rectangles) {
            widths.add(rectangle.width);
            heights.add(rectangle.height);
            wSum += rectangle.width;
            hSum += rectangle.height;
        }
        boolean b1 = true;
        boolean b2 = true;
        Collections.sort(widths);
        Collections.sort(heights);
        for (int i = 0; i < rectangles.length - 1; i++) {
            if (!widths.get(i).equals(widths.get(i + 1)))
                b1 = false;
            if (!heights.get(i).equals(heights.get(i + 1)))
                b2 = false;
        }
        if (!b2)
            wSum -= widths.get(0);
        if (!b1)
            hSum -= heights.get(0);

        totalArea = calculateTotalArea(rectangles);
        gridWidth = hSum;
        gridHeight = wSum;
        grid = new String[gridHeight][gridWidth];

        generatePositioningStates(0);

        if (possiblePositioningStates.isEmpty()) {
            System.out.println("No possible positioning states.");
        } else {
            System.out.println("Possible positioning states:");
//            for (String[][] state : possiblePositioningStates) {
//                printGrid(state);
//                System.out.println();
//            }
            System.out.println("Possible positioning states number:" + possiblePositioningStates.size());
        }
        long endTime = System.nanoTime();
        System.out.println("Elapsed time: " + TimeUnit.NANOSECONDS.toMillis(endTime - startTime) / 1000 + "."
                + (TimeUnit.NANOSECONDS.toMillis(endTime - startTime) - TimeUnit.NANOSECONDS.toSeconds(endTime - startTime) * 1000)
                + " seconds");
    }

    public static void generatePositioningStates(int index) {

        if (index == rectangles.length) {
            possiblePositioningStates.add(cloneGrid(grid));
            return;
        }

        int rowMax = gridHeight;
        int colMax = gridWidth;

        if (index != 0) {
            int lastUsedRow = 0, lastUsedCol = 0;

            for (int i = 0; i < gridHeight; i++)
                for (int j = 0; j < gridWidth; j++) {
                    if (grid[i][j] != null) {
                        lastUsedRow = Math.max(lastUsedRow, i + 1);
                        lastUsedCol = Math.max(lastUsedCol, j + 1);
                    }
                }
            rowMax = Math.min(rowMax, totalArea / lastUsedCol + 1);
            colMax = Math.min(colMax, totalArea / lastUsedRow + 1);
        }

        for (int row = 0; row < rowMax; row++) {
            for (int col = 0; col < colMax; col++) {
                if (canPlaceRectangle(row, col, rectangles[index])) {
                    placeRectangle(row, col, rectangles[index]);
                    if (checkSpaces(index))
                        generatePositioningStates(index + 1);
                    removeRectangle(row, col, rectangles[index]);
                }
            }
        }
    }

    public static boolean checkSpaces(int index) {
        int lastUsedRow = 0;
        int lastUsedCol = 0;
        for (int i = 0; i < gridHeight; i++)
            for (int j = 0; j < gridWidth; j++)
                if (grid[i][j] != null) {
                    lastUsedRow = Math.max(lastUsedRow, i + 1);
                    lastUsedCol = Math.max(lastUsedCol, j + 1);
                }
        int space = lastUsedRow * lastUsedCol;

//        int remindsSpace =calculateTotalArea2(Arrays.copyOfRange(rectangles, index, rectangles.length));
//        int placedSpace =calculateTotalArea2(Arrays.copyOfRange(rectangles, 0, index));
//        if((space-placedSpace)>remindsSpace)
//            return false;
//        if((space-placedSpace)>totalArea)
//            return false;

        return space <= totalArea;
    }

    public static boolean canPlaceRectangle(int row, int col, Rectangle rectangle) {

        if (row + rectangle.height > gridHeight || col + rectangle.width > gridWidth) {
            return false;
        }

        for (int i = row; i < row + rectangle.height; i++) {
            for (int j = col; j < col + rectangle.width; j++) {
                if (grid[i][j] != null) {
                    return false;
                }
            }
        }

        return true;
    }

    public static void placeRectangle(int row, int col, Rectangle rectangle) {
        for (int i = row; i < row + rectangle.height; i++) {
            for (int j = col; j < col + rectangle.width; j++) {
                grid[i][j] = rectangle.name;
            }
        }
    }

    public static void removeRectangle(int row, int col, Rectangle rectangle) {
        for (int i = row; i < row + rectangle.height; i++) {
            for (int j = col; j < col + rectangle.width; j++) {
                grid[i][j] = null;
            }
        }
    }

    public static int calculateTotalArea(Rectangle[] rectangles) {
        int maxArea = 0;
        for (Rectangle rectangle : rectangles) {
            maxArea += rectangle.width * rectangle.height;
        }
        return maxArea;
    }
    public static int calculateTotalArea2(Rectangle  []rectangles) {
        int maxArea = 0;
        for (Rectangle rectangle : rectangles) {
            maxArea += rectangle.width * rectangle.height;
        }
        return maxArea;
    }
        public static String[][] cloneGrid(String[][] grid) {
        String[][] clone = new String[grid.length][grid[0].length];
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                clone[i][j] = grid[i][j];
            }
        }
        return clone;
    }

    public static void printGrid(String[][] grid) {
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                System.out.print((grid[i][j] != null ? grid[i][j] : " ") + " ");
            }
            System.out.println();
        }
    }
}
the first code is generating states for putting the chars and it has their dimenthins in the string in main so that each expresson in the output is a pusitioning state for the nodes in a grid while - means go to new row and continue palcing and | means go to new colomn and continue placeing and the placing most be done as the aalgurithm in the seconf code but no need to try all grid dimantion states because the grid dimen.. could be calclated for each expression and its nodes dimenchins try to places all the expeations each one aloe and give ne the placing output all in java