a repeating string is a concatenation of two identical strings. For example, abcabc is a repeating string of length 6, while abcba does not have a repeating string.
given any string, find the longest repeating substring.
input description :
enter a string s, where s is less than 1e4 and contains only Numbers and letters.
output description :
outputs an integer representing the length of the longest repeating substring of s. If it does not exist, 0
is output
input example 1:
xabcabcx
output example 1:
6
def getMaxRepeatSubstringLength(inputStr):
length = len(inputStr)
for i in reversed(range(length//2)):
count = 0
for j in range(length - i):
if inputStr[j] == inputStr[j+i]:
count = count + 1
else:
if length - j <= 2 * i:
break
count = 0
if count == i:
return count * 2
return 0
inputStr = input()
print(getMaxRepeatSubstringLength(inputStr))
1. First assume the length of the longest repeating substring is I
2. Judge whether the length of the assumption is I is true
3. The longest repeating substring cannot be more than half of the total length
4. In the process of determining whether the hypothesis is true, if the remaining length to be determined is less than 2* I, the hypothesis must be false
5. Since it is the judgment of the eldest string, it is reasonable to assume that I is from large to small, and once it is met, you can withdraw directly. Whereas the assumption from small to large is to check all the cases.
Read More:
- JavaScript realizes the longest substring of non repeated characters
- TSLint:object access via string literals is disallowedtslint(no-string-literal)
- Finding the longest connection path of a string
- Unity short string intercepts long string
- In Java, int is converted to string, and zero is added before the number of bits is insufficient
- Python: How to Fix “Ord() expected string of length 1, but int found”
- Differences between length() size() and C strlen() of C + + string member functions
- error: ‘to_string’ is not a member of ‘std’
- C++ string substr()
- After vscode installs ts, TSC compiles and generates JS, and an error is reported (repeated variables, etc.)
- Java encrypts the string with MD5
- Split keyword in ABAP when the separator is at the beginning and end of the string
- On the intern () method of string class in Java
- To solve the problem of repeated password input when operating sourcetree on MAC
- Error resolution of ‘string’ does not name a type
- How to wrap a long string in MATLAB
- The Vue mobile terminal cannot use string.replaceall, and the error message is blank
- Mybatis error,There is no getter for property named ‘xx’ in ‘class java.lang.String The solution
- C++ foundation — clear/erase/pop of string class_back
- Caused by the “error string in namespace std does not name a type” error