finding all distinct substring of a string

前端 未结 6 1787
借酒劲吻你
借酒劲吻你 2021-01-14 21:41

hello guys i was given homework problem where it asks me to find all distinct substring of a string. I have implemented a method which will tell you all the substrings of s

6条回答
  •  不要未来只要你来
    2021-01-14 22:02

    public ArrayList getAllUniqueSubset(String str) {
            ArrayList set = new ArrayList();
            for (int i = 0; i < str.length(); i++) {
                for (int j = 0; j < str.length() - i; j++) {
                    String elem = str.substring(j, j + (i+1));
                    if (!set.contains(elem)) {
                        set.add(elem);
                    }
                }
            }
            return set;
        }
    

提交回复
热议问题