相同字符串【面试题】
我感觉还是集合用着舒服
/*
* Copyright (c) 2006, 2022, wuyahan 编写
*
*/
package cn.lele.stropt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* <p>Project: CrossFireAndLive - Main
* <p>Powered by wuyahan On 2022-12-28 09:41:51
*
* @author wuyahan [tianwenle2000@163.com]
* @version 1.0
* @since 17
*/
public class Main {
public static void main(String[] args) {
String[] strs = {"eat", "book", "tea", "tan", "ate", "nat", "bat", "bkoo"};
System.out.println("Arrays.toString(strs) = " + Arrays.toString(strs));
String[] strings = strs[0].split("");
List<String> list = new ArrayList<>();
List<String> result = new ArrayList<>();
// 如果之前检测到相同的单词 则跳过 ,继续没有检测过的
for (String str : strs) {
if (!list.contains(str)) {
List<String> eachOne = eachOne(strs, str.split(""));
result.add(Arrays.toString(eachOne.toArray()));
list.addAll(eachOne);
}
}
System.out.println("result = " + result);
}
// 获取每次循环到的相同字母的集合
private static List<String> eachOne(String[] strs, String[] strings) {
List<String> list = new ArrayList<>();
for (String str : strs) {
int i;
for (i = 0; i < strings.length; i++) {
if (!str.contains(strings[i])) {
break;
}
}
if (i == strings.length) {
list.add(str);
}
}
return list;
}
}