PAT乙级1003 测试点2 不通过 JAVA
原题链接:https://pintia.cn/problem-sets/994805260223102976/exam/problems/994805323154440192
我的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class pat1003 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int firstP, lastT, len;
String str;
String first_str, medium_str, last_str;
while (n-- > 0) {
str = br.readLine();
len = str.length();
if (str.equals("PAT")) {
System.out.println("YES");
continue;
}
if (str.contains("P") && str.contains("T")) {
firstP = str.indexOf('P');
lastT = str.indexOf('T');
first_str = str.substring(0, firstP);
medium_str = str.substring(firstP + 1, lastT);
last_str = str.substring(lastT + 1, len);
if (OnlyA(first_str) && OnlyA(medium_str) && OnlyA(last_str) && medium_str.contains("A")
&& first_str.length() * medium_str.length() == last_str.length()) {
System.out.println("YES");
continue;
}
}
System.out.println("NO");
}
br.close();
}
public static boolean OnlyA(String s) {
if (s.equals(""))
return true;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) != 'A')
return false;
}
return true;
}
}