In java. Only uses one loop, so more efficient than two loops. No arbitrary arrays, or other wastes of space/time. Also, a stringbuffer works in O(n) time, whereas string concatenation works in O(n^2), so that's why a stringbuffer was used. There are multiple ways of doing this algorithm. You could use a try-catch statement with Integer.parseInt(), or check ASCII codes, or maintain a list of numerical digits and check if a character is in that list. Any of these will work, just choose the best version for the language you're using.
The pseudocode for the algorithm is as follows, in case you need to translate to another language:
Separate(s):
create a new string to collect the alpha characters
for every character in the string:
....if the character is a numerical digit:
........print it on a single line
....else:
........add it to the string
print the string on a new line
/**
* In java. Prints numbers first on one line,
* then alpha characters on the next line,
* both in the order they appear.
**/
public static void separate(String s) {
StringBuffer alphas = new StringBuffer();
alphas.append("\n");
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (Character.isDigit(c)){
System.out.print(c);
}
else {
alphas.append(c);
}
}
alphas.append("\n");
System.out.print(alphas.toString());
}
Running Class.separate( "O2S7JD5GO5U0N6923O345L0N1MW3E2" ); outputs
2755069233450132
OSJDGOUNOLNMWE