Untitled
unknown
plain_text
a year ago
1.9 kB
2
Indexable
Never
// Online Java Compiler // Use this editor to write, compile and run your Java code online import java.util.*; class HelloWorld { public static String reverseDigits(String input){ ArrayList<Integer> digitsIndices = new ArrayList<Integer>(); ArrayList<Integer> charIndices = new ArrayList<Integer>(); ArrayList<Character> digits = new ArrayList<Character>(); ArrayList<Character> characters = new ArrayList<Character>(); // N for(int i=0; i < input.length(); i++){ char c = input.charAt(i); if(Character.isDigit(c)){ digitsIndices.add(i); digits.add(new Character(c)); } else { charIndices.add(i); characters.add(new Character(c)); } } // N Collections.reverse(digits); Character[] resultArray = new Character[input.length()]; // M for(int i=0; i < digitsIndices.size(); i++){ int index = digitsIndices.get(i); Character value = digits.get(i); resultArray[index] = value; } // P for(int i=0; i < charIndices.size(); i++){ int index = charIndices.get(i); Character value = characters.get(i); resultArray[index] = value; } // M + P = N StringBuilder output = new StringBuilder(); // N for (int i=0; i < resultArray.length; i++){ output.append(resultArray[i]); } return output.toString(); } public static void main(String[] args) { System.out.println(reverseDigits("abcdefg")); // System.out.println(reverseDigits("ab4c13")); } }