给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按“PATestPATest….”这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按PATest的顺序打印,直到所有字符都被输出。
输入格式:
输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
输入样例:
1 |
redlesPayBestPATTopTeePHPereatitAPPT |
输出样例:
1 |
PATestPATestPTetPTePePee |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String string = in.nextLine(); in.close(); List<Character> PList = new ArrayList<>(); List<Character> AList = new ArrayList<>(); List<Character> TList = new ArrayList<>(); List<Character> eList = new ArrayList<>(); List<Character> sList = new ArrayList<>(); List<Character> tList = new ArrayList<>(); for (int i = 0; i < string.length(); i++) { switch (string.charAt(i)) { case 'P': PList.add(string.charAt(i)); break; case 'A': AList.add(string.charAt(i)); break; case 'T': TList.add(string.charAt(i)); break; case 'e': eList.add(string.charAt(i)); break; case 's': sList.add(string.charAt(i)); break; case 't': tList.add(string.charAt(i)); break; } } int maxSize = PList.size(); if (AList.size() > maxSize) { maxSize = AList.size(); } if (TList.size() > maxSize) { maxSize = TList.size(); } if (eList.size() > maxSize) { maxSize = eList.size(); } if (sList.size() > maxSize) { maxSize = sList.size(); } if (tList.size() > maxSize) { maxSize = tList.size(); } for (int i = 0; i < maxSize; i++) { if (i < PList.size()) { System.out.print(PList.get(i)); } if (i < AList.size()) { System.out.print(AList.get(i)); } if (i < TList.size()) { System.out.print(TList.get(i)); } if (i < eList.size()) { System.out.print(eList.get(i)); } if (i < sList.size()) { System.out.print(sList.get(i)); } if (i < tList.size()) { System.out.print(tList.get(i)); } } } } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼