Topics are as follows:
Return the number of distinct non-empty substrings of
text
that can be written as the concatenation of some string with itself.Example 1:
Input: text = "abcabcabc" Output: 3 Explanation: The 3 substrings are "abcabc", "bcabca" and "cabcab".Example 2:
Input: text = "leetcodeleetcode" Output: 2 Explanation: The 2 substrings are "ee" and "leetcodeleetcode".Constraints:
1 <= text.length <= 2000
text
has only lowercase English letters.
Problem-solving ideas: This title is really wonderful, I thought it was to find out in line with (xxx) * substring such formats n, did not think it was (xxx) * 2 format, which means that, into a string two front portions, two values are the same part. Because text.length <= 2000, so direct violence calculate it.
code show as below:
class Solution(object): def distinctEchoSubstrings(self, text): """ :type text: str :rtype: int """ res = 0 dic = {} # dp = [[0]* len(text) for _ in text] for i in range(len(text)): for j in range(i + 1, len(text)): if (j - i + 1) % 2 != 0: continue mid = (i+j) / 2 #print text[i:mid],text[mid:j+1] v1 = text[i:mid+1] v2 = text[mid+1:j+1] if v1 == v2 and v1 not in dic: #print v1,v2 res += 1 dic[v1] = 1 return res