这个问题不是很清楚,但我会提供一个与我见过的其他问题不同的解决方案:
您也可以使用 list removeAll 来比较两个列表之间的常用字母...
listA.removeAll(listB); //listA will contain any letters not common to listB
if (listA.size() == 0) {
return true;
}
测试输出:
apppppppleeeeeeee <-- Input
[a, p, p, p, p, p, p, p, l, e, e, e, e, e, e, e, e] //String split 1
[a, p, p, l, e] //String split 2
letters are the same
appleeeeeef <-- Input
[a, p, p, l, e, e, e, e, e, e, f]
[a, p, p, l, e]
contains different letters
代码:
public class Main {
public Main () {
Scanner scan;
scan = new Scanner(System.in);
String input = scan.nextLine();
if(check(split(input), split("apple"))) {
System.out.println("letters are the same");
}
else {
System.out.println("contains different letters");
}
}
public boolean check(ArrayList<String> listA, ArrayList<String> listB) {
listA.removeAll(listB); //listA will contain any letters not common to listB
if (listA.size() == 0)
return true;
return false;
}
public ArrayList<String> split(String word) {
String[] splitMe = word.split("(?!^)");
ArrayList<String> splitList = new ArrayList<String>();
for (int i = 0; i < splitMe.length; i++) {
splitList.add(splitMe[i]);
}
System.out.println(splitList);
return splitList;
}
public static void main(String[] args) {
Main main = new Main();
}
}