count distinct number of substrings for the given string

import java.util.*;

public class DistinctSubstring {

public static int distinctSubstring(String str)
{ Set<String> result = new HashSet<String>();

for (int i = 0; i <= str.length(); i++) {
for (int j = i + 1; j <= str.length(); j++) {
result.add(str.substring(i, j));
}
}

return result.size();
}


public static void main(String[] args)
{
    Scanner s=new Scanner(System.in);
String str =s.nextLine();
System.out.println(distinctSubstring(str));
}
}
inputL
abc
output: 10

No comments:

Post a Comment