题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
没想到用同一大小写的方式来排序,但是想到了另一个方式来做
import java.io.IOException;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
char[] chars = line.toCharArray();
Set<Integer> notWordIndex = new HashSet<>();
String[] strs = new String[26];
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
int index = (c >= 'a' && c <= 'z') ? c - 'a' : c - 'A';
String s = strs[index];
if (s == null) {
strs[index] = c + "";
} else {
strs[index] = s + c;
}
} else {
notWordIndex.add(i);
}
}
List<Character> letterList = new ArrayList<>();
for (String s : strs) {
if (s != null) {
for (char c : s.toCharArray()) {
letterList.add(c);
}
}
}
StringBuilder result = new StringBuilder();
for (int i = 0, j = 0; i < chars.length; i++) {
if (notWordIndex.contains(i)) {
result.append(chars[i]);
} else {
result.append(letterList.get(j++));
}
}
System.out.println(result);
}
}


