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
| class Solution { public String makeGood(String s) { int n = s.length(); ArrayList<Character> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(s.charAt(i)); }
while (judge(list)) { for (int i = 1; i < list.size(); i++) { Character pre = list.get(i - 1); Character cur =list.get(i); if ((Character.toUpperCase(pre) == cur || pre == Character.toUpperCase(cur)) && pre != cur){ list.remove(i-1); list.remove(i-1); i--; } } }
StringBuffer sb = new StringBuffer(); for (int i = 0; i < list.size(); i++) { sb.append(list.get(i)); }
return sb.toString(); }
public boolean judge(List<Character> list){ for (int i = 1; i < list.size(); i++) { Character pre = list.get(i - 1); Character cur =list.get(i); if ((Character.toUpperCase(pre) == cur || pre == Character.toUpperCase(cur)) && pre != cur){ return true; } } return false; } }
|