Partially match strings in case of List.contains(String)

后端 未结 9 863
夕颜
夕颜 2020-12-10 11:23

I have a List

List list = new ArrayList();
list.add(\"ABCD\");
list.add(\"EFGH\");
list.add(\"IJ KL\")         


        
相关标签:
9条回答
  • 2020-12-10 12:11

    Perhaps you want to put each String group into a HashSet, and by fragment, I mean don't add "IJ KL" but rather add "IJ" and "KL" separately. If you need both the list and this search capabilities, you may need to maintain two collections.

    0 讨论(0)
  • 2020-12-10 12:12

    You can iterate over the list, and then call contains() on each String.

    public boolean listContainsString(List<string> list. String checkStr)
    {
        Iterator<String> iter = list.iterator();
        while(iter.hasNext())
        {
            String s = iter.next();
            if (s.contain(checkStr))
            {
                return true;
            }
        }
        return false;
    }
    

    Something like that should work, I think.

    0 讨论(0)
  • 2020-12-10 12:18

    If suggestion from Roadrunner-EX does not suffice then, I believe you are looking for Knuth–Morris–Pratt algorithm.

    Time complexity:

    • Time complexity of the table algorithm is O(n), preprocessing time
    • Time complexity of the search algorithm is O(k)

    So, the complexity of the overall algorithm is O(n + k).

    • n = Size of the List
    • k = length of pattern you are searching for

    Normal Brute-Force will have time complexity of O(nm)

    Moreover KMP algorithm will take same O(k) complexity for searching with same search string, on the other hand, it will be always O(km) for brute force approach.

    0 讨论(0)
提交回复
热议问题