[Leetcode] Partition Labels

Source: Internet
Author: User

A string of S lowercase letters is given. We want to partition this string into as many parts as possible so the each letter appears in at the most one part, and retur n a list of integers representing the size of these parts.

Example 1:

input:s = "Ababcbacadefegdehijhklij" Output: [9,7,8]explanation:the partition is "Ababcbaca", "Defegde", "Hijhklij". This is a partition so, each letter appears in the most one part. A partition like "Ababcbacadefegde", "Hijhklij" are incorrect, because it splits S into less parts.

Note:

    1. SWould has length in range [1, 500] .
    2. SWould consist of lowercase letters ( ‘a‘ to ‘z‘ ) only.
Analysis: Topic translation: Given a string, it is required to fragment the string so that the characters appearing in each slice appear only in this shard. The first idea: Save the number of occurrences of each string with a map, and if you go to a character where all the preceding characters correspond to a value of 0, then the Shard is found. According to this principle, the following code can be written:
1 classSolution {2      PublicList<integer>partitionlabels (String S) {3map<character,integer> map =NewHashmap<>();4Set<character> set =NewHashset<>();5list<integer> list =NewArraylist<>();6 7          for(CharC:s.tochararray ())8Map.put (C,map.getordefault (c,0) +1);9         //System.out.println (map);Ten  One         intleft = 0; A         intCount = 0; -          while(Left <s.length ()) { -             Charc =S.charat (left); the Set.add (c); -Map.put (C,map.get (c)-1); -Count + = 1; -             BooleanIse =true; +              for(CharTemp:set) { -                 if(Map.get (temp)! = 0) ise =false; +             } A             if(ise) { at List.add (count); -Count = 0; - set.clear (); -             } -Left + +; -         } in         returnlist; -     } to}

Run time 42ms, very time-consuming. But this method is the most intuitive method. Below is an analysis of how to improve.

The second idea: the previous method is to keep the map-1 operation, and then determine whether it is 0. This topic can also use map to hold the last position of each character, and use a variable to hold the farthest position of the previous characters. If the cur pointer goes to this position, it means that the previous one has been accessed, and the Shard is obtained. Show Me the Code:

1 classSolution {2      PublicList<integer>partitionlabels (String S) {3map<character,integer> map =NewHashmap<>();4list<integer> list =NewArraylist<>();5         6          for(inti = 0; I < s.length (); i + + )7 Map.put (S.charat (i), i);8 9         intleft = 0, right = 0, cur = 0;Ten          while(Cur <s.length ()) { One             Charc =S.charat (cur); Aright =Math.max (Right,map.get (c)); -             if(cur = =Right ) { -List.add (right-left+1); theleft = right+1; -             } -cur++; -         } +         returnlist; -     } +}

Run time 14ms, beat 36.7%, or very slow. Why is it?

The third idea: actually also does not scattered the complete idea, to the above to improve, because the map workload is larger, therefore may wish to use a 26-length array instead of the map (this method is very common when using the map and the string).

1 classSolution {2      PublicList<integer>partitionlabels (String S) {3list<integer> list =NewArraylist<>();4         int[] A =New int[26];5          for(inti = 0; I < s.length (); i + + )6A[s.charat (i)-' a '] =i;7 8         intleft = 0, right = 0, cur = 0;9          while(Cur <s.length ()) {Ten             Charc =S.charat (cur); Oneright = Math.max (right,a[c-' a ']); A             if(cur = =Right ) { -List.add (right-left+1); -left = right+1; the             } -cur++; -         } -         returnlist; +     } -}

Run time 12ms, beat 52.83%. I've done my best.

Summary: The key is to think about how many times the last occurrence of each character is saved in a map, and how to determine if the Shard condition is met during the loop.

[Leetcode] Partition Labels

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.