1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| class Solution { public String sortVowels(String s) { int n = s.length(); StringBuilder sb = new StringBuilder(s); List<Integer> emptyIndex = new LinkedList<>(); int[] count = new int[128]; for (int i = 0; i < n; ++i) { char ch = s.charAt(i); if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') { emptyIndex.add(i); ++count[ch]; } } int[] temp = emptyIndex.stream().mapToInt(i->i).toArray(); for (int i = 0; i < temp.length; ++i) { int index = temp[i]; char ch = ' '; if (count['A'] > 0) { --count['A']; sb.setCharAt(index, 'A'); } else if (count['E'] > 0) { sb.setCharAt(index, 'E'); --count['E']; } else if (count['I'] > 0) { sb.setCharAt(index, 'I'); --count['I']; } else if (count['O'] > 0) { sb.setCharAt(index, 'O'); --count['O']; } else if (count['U'] > 0) { sb.setCharAt(index, 'U'); --count['U']; } else if (count['a'] > 0) { sb.setCharAt(index, 'a'); --count['a']; } else if (count['e'] > 0) { sb.setCharAt(index, 'e'); --count['e']; } else if (count['i'] > 0) { sb.setCharAt(index, 'i'); --count['i']; } else if (count['o'] > 0) { sb.setCharAt(index, 'o'); --count['o']; } else if (count['u'] > 0) { sb.setCharAt(index, 'u'); --count['u']; } }
return sb.toString(); } }
|