Nine-chapter algorithm surface test questions 34 Maximum 01 substrings

Source: Internet
Author: User

Nine Chapters count judges Net-original website

http://www.jiuzhang.com/problem/34/


Topics

There is a 01 string consisting of only 0 and 1, where the longest substring is found, making the number of 0 and 1 in the substring equal


Answer

If 0 is considered as 1, then the substring we are looking for is the longest and the substring of 0. The problem of this seed string summation is generally solved by prefix and method. With Sum[i] representing the number of the first I and, the problem of the model converted to, find I and J, meet Sum[i] and sum[j] equal, and |i-j| the largest. Using a hash table as a secondary data structure, the hash table records the smallest i when a sum is obtained. Traverse Sum[i] From left to right, look in the hash table for existence, if present, record the distance difference between Hash[sum[i]] and I, otherwise hash[sum[i] = I. The maximum distance difference is obtained at the end of a single traversal, and the specific segment can be obtained.

Interviewer AngleSimilar topics are "and the largest Subarray", "and a subarray of 0", "and the nearest sub-array of 0". The common solution is through prefixes and sum[i].



Nine-chapter algorithm surface test questions 34 Maximum 01 substrings

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.