I want ab to be returned as ba but my output is a a. I understand why I get that output. If a[i] < a[j] (if 1st index < 2nd index, which is true because a < b, then replace b (a[j]) with a (a[i])).
I just can't wrap my head around how I would go about swapping those two letters with arrays.
How would I be able to accomplish this?
public static void solution(String A) {
char[] a = A.toCharArray();
for(int i = 0; i < a.length; i++) {
for(int j = i + 1; j < a.length; j++) {
if(a[i] < a[j]) {
a[j] = a[i];
}
}
}
for(int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
public static void main(String args[]) {
String A = "ab";
solution(A);
}