finding all distinct substring of a string

前端 未结 6 1711
借酒劲吻你
借酒劲吻你 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 21:57

    Here the example with a Set

    public int printSubstrings1(int length) {
        Set set = new HashSet();
        for(int i=0; i < text.length() - length + 1; i++) {
            String sub = text.substring(i,length+i);
            set.add(sub);
        }
        for (String str : set) {
            System.out.println(str);
        }
        return set.size();
    }
    

提交回复
热议问题