简体   繁体   English

如何从字符串列表中找到最长的通用后缀并在Java中返回结果后缀长度?

[英]How to find longest common suffix from a list of strings and return the resulting suffix length in java?

请在ArrayList或LinkedList中考虑以下内容:[Gloucestershire,Hampshire,Yorkshire,Lancashire] shire是长度为5的最长的通用后缀。输出应为5我如何编写一种方法来实现上述和返回长度

Try this. 尝试这个。
Explanation is in the comments 注释中有解释

package javaapplication1;
public class SegmentTree {

    public static void main(String[] args) {
        String[] array = {"Gloucestershire", "Hampshire", "Yorkshire", "Lancashire"};
        int i,j;
        int min=10000000;

        //reversing the strings and finding the length of smallest string
        for(i=0;i<(array.length);i++)
        {
          if(array[i].length()<min)
              min=array[i].length();


         StringBuilder input1 = new StringBuilder();
         input1.append(array[i]);
         array[i] = input1.reverse().toString();
        }

        //finding the length of longest suffix

        for(i=0;i<min;i++)
        {
          for(j=1;j<(array.length);j++)
          {
              if(array[j].charAt(i)!=array[j-1].charAt(i))
              {
                break;
              }
          }
          if(j!=array.length)
              break;
        }

        System.out.println(i);
    }
}

What I am doing here is,first checking the last elem of all strings, then 2nd last and so on. 我在这里所做的是,首先检查所有字符串的最后一个元素,然后检查第二个等等。

Time Complexity: O(n*m) , 时间复杂度:O(n * m)
n=number of strings, m=length of smallest string n =字符串数,m =最小字符串的长度

Guava has a helper function called Strings.commonSuffix(CharSequence a, CharSequence b) that could be used in your algorithm. 番石榴有一个名为Strings.commonSuffix(CharSequence a, CharSequence b)的辅助函数,可以在您的算法中使用。 I know adding dependency like Guava only to have this function can be overkill - in this case you can take a look at the source code to see how this function is implemented and you can move this implementation to your program. 我知道添加像Guava这样的依赖项仅具有该功能可能会过大-在这种情况下,您可以查看源代码以了解如何实现此功能,并将此实现移至程序中。 Then your program could look like this: 然后您的程序可能如下所示:

import com.google.common.base.Strings;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CommonSuffixLengthMain {

    public static void main(String[] args) throws IOException {

        assert 5 == commonSuffixLength(Arrays.asList("Gloucestershire", "Hampshire", "Yorkshire", "Lancashire"));

        assert 2 == commonSuffixLength(Arrays.asList("abc", "dbc", "qebc", "webc"));

        assert 0 == commonSuffixLength(Collections.emptyList());

        assert 0 == commonSuffixLength(Collections.singletonList("abc"));

        assert 0 == commonSuffixLength(Arrays.asList("abc", "def", "zxc"));
    }

    private static int commonSuffixLength(final List<String> strings) {
        int result = 0;

        if (strings == null || strings.size() < 2) {
            return result;
        }

        for (int i = 0; i < strings.size() - 1; i++) {
            String prefix = Strings.commonSuffix(strings.get(i), strings.get(i + 1));
            result = result == 0 ?
                    prefix.length() :
                    Math.min(prefix.length(), result);
        }

        return result;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM