Two examples of string lookup in Python data structure

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.