Java queue with dry run
s = "12345" and t = "12435":\nInitial Setup:\nString s: "12345"\nString t: "12435"\nQueues: We initialize 10 queues for digits 0 through 9.\nPopulate Queues wit
s = "12345" and t = "12435":\nInitial Setup:\nString s: "12345"\nString t: "12435"\nQueues: We initialize 10 queues for digits 0 through 9.\nPopulate Queues with Indices from s:\nFor s[0] = \'1\', add index 0 to queue 1.\nFor s[1] = \'2\', add index 1 to queue 2.\nFor s[2] = \'3\', add index 2 to queue 3.\nFor s[3] = \'4\', add index 3 to queue 4.\nFor s[4] = \'5\', add index 4 to queue 5.\nQueues after populating:\n\nmakefile\nCopy code\n0: []\n1: [0]\n2: [1]\n3: [2]\n4: [3]\n5: [4]\n6: []\n7: []\n8: []\n9: []\nProcess Target String t:\nStep 1: t[0] = \'1\'\nTarget Digit: 1\nCurrent Queue for \'1\': [0]\nCheck Smaller Digits: No smaller digits (0) have positions.\nUse Index: 0 (remove it from queue 1)\nQueues after Step 1:\n\nmakefile\nCopy code\n0: []\n1: []\n2: [1]\n3: [2]\n4: [3]\n5: [4]\n6: []\n7: []\n8: []\n9: []\nStep 2: t[1] = \'2\'\nTarget Digit: 2\nCurrent Queue for \'2\': [1]\nCheck Smaller Digits: No smaller digits (0, 1) have positions before the current digit\'s position.\nUse Index: 1 (remove it from queue 2)\nQueues after Step 2:\n\nmakefile\nCopy code\n0: []\n1: []\n2: []\n3: [2]\n4: [3]\n5: [4]\n6: []\n7: []\n8: []\n9: []\nStep 3: t[2] = \'4\'\nTarget Digit: 4\nCurrent Queue for \'4\': [3]\nCheck Smaller Digits:\nDigit 0: No positions.\nDigit 1: No positions.\nDigit 2: No positions.\nDigit 3: Position [2] (before 3). This is where the issue arises.\nSince digit 3 appears before digit 4 in the source string s and must be moved to the right of 4 in the target string t, the transformation cannot be performed.\n\nThus, the correct action here is to return false.\n\n# Code\n```\nclass Solution {\n    public boolean isTransformable(String s, String t) {\n        Deque<Integer>[] positions = new Deque[10];\n        Arrays.setAll(positions, k -> new ArrayDeque<>());\n\n        // Fill each queue with indices of digits in string s\n        for (int i = 0; i < s.length(); i++) {\n            int digit = s.charAt(i) - \'0\'; // Get digit at char position i\n            positions[digit].offer(i); // Add index to corresponding digit queue\n        }\n\n        // Process the target string to see if it is transformable from source string\n        for (int i = 0; i < t.length(); i++) {\n            int targetDigit = t.charAt(i) - \'0\'; // Target digit to search for\n\n            // If there are no positions left for this digit, s cannot be transformed into t\n            if (positions[targetDigit].isEmpty()) {\n                return false;\n            }\n\n            // Check if any smaller digit appears after the current digit in the source\n            for (int j = 0; j < targetDigit; j++) {\n                // If there is a smaller digit and it comes before the current digit in source s\n                if (!positions[j].isEmpty() && positions[j].peek() < positions[targetDigit].peek()) {\n                    return false; // s cannot be transformed into t\n                }\n            }\n\n            // If position is valid, remove it as it is \'used\' for transformation\n            positions[targetDigit].poll();\n        }\n      \n        // If all checks pass, s is transformable into t\n        return true;\n    }\n}\n```
['Queue', 'Java']