Find the longest consecutive number substring problem description in a string
Looks for the longest numeric string in the given strings, returning its starting subscript, length, and string. For example:
Input:abc12345cd123ef234567df
Output:15 6 234567
Implement
"' Finds the longest numeric string in the given strings, returns its starting subscript, length, and string. For example: input : abc12345cd123ef234567dfoutput:15 6 234567 "def find_max_length_str (string): str_length = Len ( string) i = 0 max_length = 0 num_length = 0 start_num = 0 while i < str_length: if string[i] &G T ' 0 ' and string[i] < ' 9 ': start_num = i num_length = 0 while i < str_length and string[i] > ' 0 ' and S Tring[i] < ' 9 ': i + = 1 num_length + 1 if num_length! = 0 and Max_length <= num_length: Max_leng th = num_length i + = 1 return start_num, num_length, String[start_num:start_num + num_length]
Just walk through the string, time complexity: O (N)
finds the longest consecutive identical number string in a numeric literal Problem Descriptionfinds the longest consecutive identical string in a given number string, returning its starting subscript, length, and substring. For exampleinput:11233344555666666
Output:11 6 666666Implement
"' Finds the longest contiguous string in the number string, returns its starting subscript, length and substring input : 11233344555666666output:11 6 666666" def find_same_sequence_num (String): str_length = Len (string) i = 0 max_length = 0 start_num = 0 num_length = 0 while i < Str_lengt H: If i + 1 < str_length and string[i] = = string[i + 1]: start_num = i num_length = 1 while i + 1 < Str_length and string[i] = = string[i + 1]: i + = 1 num_length + 1 if num_length! = 0 and Max_length <= Num_length: max_length = num_length i + = 1 return start_num, num_length, String[start_num:start_num + Num_length]
also just traverse through the string, the time complexity is O (n)
Two examples of string lookup in Python data structure