class Solution { public int distinctEchoSubstrings(String text) { /* Q66 of dp playlist , echo substring(ess) */ int n = text.length(); //make a HS to store distinct echo substrings HashSet<String> set = new HashSet<>(); //checking all ess of length 2->n/2 for(int len = 1 ; len <= n/2 ; len++){ /* we keep checking characters at a gap of length and increasing count if chars are equal we keep the count going for potential ess ,if chars are not equal our current potential ess is ruined and we start checking again for new ess from count = 0 if count = len , we add the substring in the HS and count-- to check if the next characters are making and ess , basically it means deleting a character from behind and continue checking to find next ess */ int count = 0; for(int i = 0,j = len ;j < n ; i++ , j++){ char ch1 = text.charAt(i) , ch2 = text.charAt(j); if(ch1 == ch2) count ++; else count = 0; if(count == len){ String str = text.substring(i - (len-1), j+1); set.add(str); count --; } } } System.out.print(set); return set.size(); } }