Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def long_repeat(line):
- substrings = []
- current_substring = ""
- for i, letter in enumerate(line):
- if i == 0:
- current_substring = letter
- else:
- if letter == current_substring[len(current_substring) - 1]:
- current_substring += letter
- if(i == len(line)- 1):
- substrings.append(current_substring)
- else:
- substrings.append(current_substring)
- current_substring = letter
- lengths = []
- for substring in substrings:
- lengths.append(len(substring))
- if len(lengths) > 0:
- return max(lengths)
- else:
- return 0
- def get_length_of_longest_repeated_substring(text):
- """Outputs the length of the longest substring with the same letters.
- Args:
- text: Text in which to look for longest repeated substring.
- Returns:
- Length of the longest repeated character.
- """
- repeated_substrings = []
- current_substring = ''
- previous_character = ''
- for i, current_character in enumerate(text):
- if not current_substring:
- current_substring = current_character
- previous_character = current_character
- continue
- if current_character == previous_character:
- current_substring += letter
- else:
- repeated_substrings.append(current_substring)
- current_substring = letter
- if current_substring and len(current_substring) > 1:
- repeated_substrings.append(current_substring)
- max_length = 0
- for substring in repeated_substrings:
- if len(substring) > max_length:
- max_length = len(substring)
- return max_length
- def get_length_of_longest_repeated_substring(string, n, i):
- i += 1
- while (i < n):
- current = string[i]
- previous = string[i - 1]
- if (current == previous):
- i += 1
- else:
- break
- return i - 1
- def largestSubstring(string, n):
- length = 0
- i = 0
- while (i < n):
- end = get_length_of_longest_repeated_substring(string, n, i)
- length = max(end - i + 1, length)
- i = end + 1
- return length
- #string = "aaaaa"
- #n = len(string)
- #print(largestSubstring(string, n))
- >>> 5
- #string = "sdsffffse"
- #n = len(string)
- #print(largestSubstring(string, n))
- >>> 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement