Java 确定具有共享前缀或后缀的字符串列表中每个字符串的单个唯一子字符串

Java 确定具有共享前缀或后缀的字符串列表中每个字符串的单个唯一子字符串,java,string,algorithm,Java,String,Algorithm,假设我有一个带有共享前缀和/或后缀的字符串列表 我想确定每个字符串的唯一子字符串 如果字符串既不共享前缀也不共享后缀,则返回空字符串 示例: 1 Input: { "Katie walked to the beach in her fresh new kicks", "Katie walked to the arcade in her fresh new kicks", "Katie walked to the gazebo in her fresh

假设我有一个带有共享前缀和/或后缀的字符串列表

我想确定每个字符串的唯一子字符串

如果字符串既不共享前缀也不共享后缀,则返回空字符串

示例:

1
Input:
      { "Katie walked to the beach in her fresh new kicks",
        "Katie walked to the arcade in her fresh new kicks",
        "Katie walked to the gazebo in her fresh new kicks"}
Output:
      { "beach",
        "arcade",
        "gazebo"}
--------------------------------------------------------
2
Input:
      { "Eric walked home with nothing",
        "Brad jogged home with nothing",
        "Brad jogged home with seven bucks"}
Output:
      {""}
--------------------------------------------------------
3
Input:
      { "Jill had fun",
        "Jill had salad for lunch",
        "Jill had twins!" }
Output:
      {"fun",
       "salad for lunch",
       "twins!"}
--------------------------------------------------------
4
Input:
      { "Bread",
        "Butter",
        "Bill Nye" }
Output:
      {"read",
       "utter",
       "ill Nye"}
对伪代码、java代码或建议感兴趣


编辑:谢谢你们,评论者和回答者,你们帮助我更好地理解这个问题——正如前面所说的,我的问题是胡说八道。希望它现在是一致的。我认为其中一些答案是正确的,我将在几天后检查w/假设所有字符串都以相同的文本开头,“Katie Walk to the”,使用以下代码片段:

String place = "Katie walked to the YOUR_PLACE_HERE".split(" ")[4];
在一种方法中:

public String[] getPlaces(String[] strings) {
    String[] places = new String[strings.length];
    for (int i = 0; i < strings.length; i++) {
        places[i] = strings[i].split(" ")[4];
    }
    return places;
}
public String[]getPlaces(String[]strings){
String[]places=新字符串[strings.length];
for(int i=0;i
更通用的方法是:

public static String[] getPlaces(String[] strings, String splitString) {
    String[] places = new String[strings.length];
    for (int i = 0; i < strings.length; i++) {
        places[i] = strings[i].split(" ")[splitString.split(" ").length];
    }
    return places;
}
publicstaticstring[]getPlaces(String[]strings,stringsplitstring){
String[]places=新字符串[strings.length];
for(int i=0;i
假设所有字符串都以相同的文本开头,“Katie走向”,请使用以下代码段:

String place = "Katie walked to the YOUR_PLACE_HERE".split(" ")[4];
在一种方法中:

public String[] getPlaces(String[] strings) {
    String[] places = new String[strings.length];
    for (int i = 0; i < strings.length; i++) {
        places[i] = strings[i].split(" ")[4];
    }
    return places;
}
public String[]getPlaces(String[]strings){
String[]places=新字符串[strings.length];
for(int i=0;i
更通用的方法是:

public static String[] getPlaces(String[] strings, String splitString) {
    String[] places = new String[strings.length];
    for (int i = 0; i < strings.length; i++) {
        places[i] = strings[i].split(" ")[splitString.split(" ").length];
    }
    return places;
}
publicstaticstring[]getPlaces(String[]strings,stringsplitstring){
String[]places=新字符串[strings.length];
for(int i=0;i
您也可以使用regex尝试这种简单的方法

 public static void main(String[] args) {
 String[] str = new String[4];      
 str[0] = "Katie walked to the beach in her fresh new kicks";
 str[1] = "Katie walked to the arcade in her fresh new kicks";
 str[2] = "Katie walked to the gazebo in her fresh new kicks";
 str[3] = "Katie walked to the mall in her fresh new kicks";


 for (int i = 0; i<str.length; i++) {
 System.out.println(str[i].replaceAll("Katie|walked|to|the|in|her|fresh|new|kicks", ""));
 }

}
publicstaticvoidmain(字符串[]args){
字符串[]str=新字符串[4];
str[0]=“凯蒂穿着新鞋走到海滩”;
str[1]=“凯蒂穿着新鞋走进了拱廊”;
str[2]=“凯蒂穿着新鞋走向露台”;
str[3]=“凯蒂穿着新鞋走进商场”;

对于(inti=0;i您也可以使用regex尝试这种简单的方法

 public static void main(String[] args) {
 String[] str = new String[4];      
 str[0] = "Katie walked to the beach in her fresh new kicks";
 str[1] = "Katie walked to the arcade in her fresh new kicks";
 str[2] = "Katie walked to the gazebo in her fresh new kicks";
 str[3] = "Katie walked to the mall in her fresh new kicks";


 for (int i = 0; i<str.length; i++) {
 System.out.println(str[i].replaceAll("Katie|walked|to|the|in|her|fresh|new|kicks", ""));
 }

}
publicstaticvoidmain(字符串[]args){
字符串[]str=新字符串[4];
str[0]=“凯蒂穿着新鞋走到海滩”;
str[1]=“凯蒂穿着新鞋走进了拱廊”;
str[2]=“凯蒂穿着新鞋走向露台”;
str[3]=“凯蒂穿着新鞋走进商场”;
对于(inti=0;i试试这个

static List<String> uniqueString(List<String> lines) {
    List<String> uniques = new ArrayList<>();
    if (lines.size() == 0) return uniques;
    String prefix = lines.get(0);
    String suffix = lines.get(0);
    for (String s : lines) {
        while (!s.startsWith(prefix))
            prefix = prefix.substring(0, prefix.length() - 1);
        while (!s.endsWith(suffix))
            suffix = suffix.substring(1);
    }
    if (prefix.length() == 0 && suffix.length() == 0)
        uniques.add("");
    else
        for (String s : lines)
            uniques.add(s.substring(prefix.length(), s.length() - suffix.length()));
    return uniques;
}

@Test
public void testUniqueSubstrig() {
    System.out.println(uniqueString(Arrays.asList(
        "Katie walked to the beach in her fresh new kicks",
        "Katie walked to the arcade in her fresh new kicks",
        "Katie walked to the gazebo in her fresh new kicks"
    ))); // -> [beach, arcade, gazebo]
    System.out.println(uniqueString(Arrays.asList(
        "Eric walked home with nothing",
        "Brad jogged home with nothing",
        "Brad jogged home with seven bucks"
    ))); // -> []
    System.out.println(uniqueString(Arrays.asList(
        "Jill had fun",
        "Jill had salad for lunch",
        "Jill had twins!" 
    ))); // -> [fun, salad for lunch, twins!]
    System.out.println(uniqueString(Arrays.asList(
        "Bread",
        "Butter",
        "Bill Nye"
    ))); // -> [read, utter, ill Nye]
}
静态列表唯一字符串(列表行){
List uniques=new ArrayList();
if(lines.size()==0)返回唯一性;
字符串前缀=行。获取(0);
字符串后缀=行。获取(0);
用于(字符串s:行){
while(!s.startsWith(前缀))
prefix=prefix.substring(0,prefix.length()-1);
而(!s.endsWith(后缀))
后缀=后缀。子字符串(1);
}
if(前缀.length()==0&&suffix.length()==0)
唯一性。添加(“”);
其他的
用于(字符串s:行)
添加(s.substring(前缀.length(),s.length()-后缀.length());
返回单号;
}
@试验
public void testUniqueSubstrig(){
System.out.println(uniqueString(Arrays.asList(
“凯蒂穿着新鞋去海滩”,
“凯蒂穿着她的新鞋走进拱廊”,
“凯蒂穿着新鞋走向露台”
)))/->[海滩、拱廊、露台]
System.out.println(uniqueString(Arrays.asList(
“埃里克一无所有地走回家”,
“布拉德一无所获地慢跑回家”,
“布拉德用七块钱慢跑回家”
))); // -> []
System.out.println(uniqueString(Arrays.asList(
“吉尔玩得很开心”,
“吉尔午餐吃沙拉”,
“吉尔有双胞胎!”
)))/->[有趣,午餐沙拉,双胞胎!]
System.out.println(uniqueString(Arrays.asList(
“面包”,
“黄油”,
“比尔·奈”
)))
}
试试这个

static List<String> uniqueString(List<String> lines) {
    List<String> uniques = new ArrayList<>();
    if (lines.size() == 0) return uniques;
    String prefix = lines.get(0);
    String suffix = lines.get(0);
    for (String s : lines) {
        while (!s.startsWith(prefix))
            prefix = prefix.substring(0, prefix.length() - 1);
        while (!s.endsWith(suffix))
            suffix = suffix.substring(1);
    }
    if (prefix.length() == 0 && suffix.length() == 0)
        uniques.add("");
    else
        for (String s : lines)
            uniques.add(s.substring(prefix.length(), s.length() - suffix.length()));
    return uniques;
}

@Test
public void testUniqueSubstrig() {
    System.out.println(uniqueString(Arrays.asList(
        "Katie walked to the beach in her fresh new kicks",
        "Katie walked to the arcade in her fresh new kicks",
        "Katie walked to the gazebo in her fresh new kicks"
    ))); // -> [beach, arcade, gazebo]
    System.out.println(uniqueString(Arrays.asList(
        "Eric walked home with nothing",
        "Brad jogged home with nothing",
        "Brad jogged home with seven bucks"
    ))); // -> []
    System.out.println(uniqueString(Arrays.asList(
        "Jill had fun",
        "Jill had salad for lunch",
        "Jill had twins!" 
    ))); // -> [fun, salad for lunch, twins!]
    System.out.println(uniqueString(Arrays.asList(
        "Bread",
        "Butter",
        "Bill Nye"
    ))); // -> [read, utter, ill Nye]
}
静态列表唯一字符串(列表行){
List uniques=new ArrayList();
if(lines.size()==0)返回唯一性;
字符串前缀=行。获取(0);
字符串后缀=行。获取(0);
用于(字符串s:行){
while(!s.startsWith(前缀))
prefix=prefix.substring(0,prefix.length()-1);
而(!s.endsWith(后缀))
后缀=后缀。子字符串(1);
}
if(前缀.length()==0&&suffix.length()==0)
唯一性。添加(“”);
其他的
用于(字符串s:行)
添加(s.substring(前缀.length(),s.length()-后缀.length());
返回单号;
}
@试验
public void testUniqueSubstrig(){
System.out.println(uniqueString(Arrays.asList(
“凯蒂穿着新鞋去海滩”,
“凯蒂穿着她的新鞋走进拱廊”,
“凯蒂穿着新鞋走向露台”
)))/->[海滩、拱廊、露台]
System.out.println(uniqueString(Arrays.asList(
“埃里克一无所有地走回家”,
“布拉德一无所获地慢跑回家”,
“布拉德用七块钱慢跑回家”
))); // -> []
System.out.println(uniqueString(Arrays.asList(
“吉尔玩得很开心”,
“吉尔午餐吃沙拉”,
“吉尔有双胞胎!”
)))/->[有趣,午餐沙拉,双胞胎!]
System.out.println(uniqueString(Arrays.asList(
“面包”,
“黄油”,
“比尔·奈”
)))
}
忽略示例(2),它应该给出@j_random_hacker提到的输出,我们可以使用以下具有O(n)时间复杂度的简单方法:

  • 从索引0开始,转到