考虑
String example = "year, year - 1970, year - 1980, event, event - launch, event - stop, event - stop - final, 1970 - year"
代表顶级类别,中级类别,低类别。 如何删除所有内容,但大多数扩展了不同的类别? E.g:
"year" - occurs as first element in "year - 1970" and "year - 1980", so it is removed.
"event" - occurs in "event - launch", "event - stop", "event - stop - final", so it is removed.
"event - stop" occurs in "event - stop - final", so it is removed.
结果应该是:
example = "year - 1970, year - 1980, event - launch, event - stop - final, 1970 - year"
我的尝试很慢
String[] part = example.split(",");
ArrayList<String> list = new ArrayList<>();
for (String s : part) {
list.add(s);
}
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.size(); j++) {
if (list.get(j).contains(list.get(i))) {
String[] split1 = list.get(j).split("-");
String[] split2 = list.get(i).split("-");
if (split2.length < split1.length) list.remove(i);
}
}
}
答案 0 :(得分:0)
我创建了一个测试应用程序。结果如下。
input year, year - 1970, year - 1980, event, event - launch, event - stop,
event - stop - final, 1970 - year
output year - 1970, year - 1980, event - launch, event - stop - final, 1970 - year
我使用", "
分割输入行。我使用String startsWith
方法编辑了更高的类别。
以下是代码:
package com.ggl.testing;
public class RemoveSubstrings {
public static void main(String[] args) {
String example = "year, year - 1970, year - 1980, event, "
+ "event - launch, event - stop, event - stop - final, "
+ "1970 - year";
System.out.println(example);
System.out.println(substring(example));
}
public static String substring(String s) {
String[] parts = s.split(", ");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
boolean unique = true;
for (int j = i + 1; j < parts.length; j++) {
if (parts[j].startsWith(parts[i])) {
unique = false;
break;
}
}
if (unique) {
builder.append(parts[i]);
builder.append(", ");
}
}
if (builder.length() > 2) {
return builder.substring(0, builder.length() - 2).toString();
}
return builder.toString();
}
}