Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static String findSmallestNumber(String pattern) {
- if (pattern == null || pattern.isEmpty() || !pattern.matches("[MN]+")) {
- return "-1"; // Invalid input handling
- }
- StringBuilder result = new StringBuilder();
- Stack<Integer> stack = new Stack<>();
- int num = 1;
- for (int i = 0; i <= pattern.length(); i++) {
- stack.push(num++);
- if (i == pattern.length() || pattern.charAt(i) == 'N') {
- while (!stack.isEmpty()) {
- result.append(stack.pop());
- }
- }
- }
- return result.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement