String Matching in an Array

给一个string数组, 找出其中的string是其他string的substring. 一个个比较, 用set去重.

class Solution {
    public List<String> stringMatching(String[] words) {
        List<String> res = new ArrayList<>();
        Set<String> set = new HashSet<>();
        if(words == null || words.length == 0)
            return res;
        for(int i = 0; i < words.length; i++) {
            for(int j = 0; j < words.length; j++) {
                if(i != j && words[i].contains(words[j])){
                    set.add(words[j]);
                }
            }
        }
        res.addAll(set);
        return res;
    }
}