- Difficulty: Medium
- Tags: LeetCode, Medium, Trie, String, Suffix Array, Hash Function, Rolling Hash, leetcode-1698, O(n^2), O(t), 🔒
Problem
Given a string s
, return the number of distinct substrings of s
.
A substring of a string is obtained by deleting any number of characters (possibly zero) from the front of the string and any number (possibly zero) from the back of the string.
Â
Example 1:
Input: s = "aabbaba" Output: 21 Explanation: The set of distinct strings is ["a","b","aa","bb","ab","ba","aab","abb","bab","bba","aba","aabb","abba","bbab","baba","aabba","abbab","bbaba","aabbab","abbaba","aabbaba"]
Example 2:
Input: s = "abcdefg" Output: 28
Â
Constraints:
1 <= s.length <= 500
s
consists of lowercase English letters.
Â
Follow up: Can you solve this problem inO(n)
time complexity?