双循环搜索+排序优化+Set集合优化
处理输入,去除"[" "]" "" "三种字符,再split得到字符串数组
import java.util.*; public class Main { public static boolean containSet(Set<Character> set, String str){ for(int i = 0; i < str.length(); i++){ if(set.contains(str.charAt(i))){ return true; } } return false; } public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String[] s = scanner.nextLine().replace("[", "") .replace("]", "") .replace("\"", "").split(","); System.out.println(Arrays.toString(s)); Arrays.sort(s, (o1, o2) -> Integer.compare(o2.length(), o1.length())); int sum = 0; for(int i = 0; i < s.length; i++){ Set<Character> set = new HashSet<>(); s[i].chars().forEach((c) -> set.add((char) c)); for(int j = i; j < s.length; j++){ if(!containSet(set, s[j])){ sum = Math.max(s[i].length() * s[j].length(), sum); } } } System.out.println(sum); } }