@@ -0,0 +1,2163 @@
+package com.amaz;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A/**%0A * A string S of lowercase English letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.%0A *%0A * %C2%A0%0A *%0A * Example 1:%0A *%0A * Input: S = %22ababcbacadefegdehijhklij%22%0A * Output: %5B9,7,8%5D%0A * Explanation:%0A * The partition is %22ababcbaca%22, %22defegde%22, %22hijhklij%22.%0A * This is a partition so that each letter appears in at most one part.%0A * A partition like %22ababcbacadefegde%22, %22hijhklij%22 is incorrect, because it splits S into less parts.%0A *%0A */%0Apublic class PartitionLabels %7B%0A    public List%3CInteger%3E partitionLabels(String S) %7B%0A        int%5B%5D last = new int%5B26%5D;%0A        int len = S.length();%0A        for (int i = 0; i %3C len; i++) %7B%0A            // save each letter's end position%0A            last%5BS.charAt(i) - 'a'%5D = i;%0A        %7D%0A        int start = 0;%0A        int scannedMaxPos = 0;%0A        List%3CInteger%3E rst = new ArrayList%3CInteger%3E();%0A        for (int i = 0; i %3C len; i++) %7B%0A            scannedMaxPos = Math.max(scannedMaxPos, last%5BS.charAt(i) - 'a'%5D);%0A            // when the index arrive the max pos of scanned letter%0A            if (i == scannedMaxPos) %7B%0A                rst.add(i - start + 1);%0A                start = i + 1;%0A            %7D%0A        %7D%0A        return rst;%0A    %7D%0A%0A    public List%3CInteger%3E partitionLabels2(String S) %7B%0A        Map%3CCharacter,Integer%3E last = new HashMap%3C%3E();%0A        int len = S.length();%0A        for (int i = 0; i %3C len; i++) %7B%0A            // save each letter's end position%0A            last.put(S.charAt(i), i);%0A        %7D%0A        int start = 0;%0A        int scannedMaxPos = 0;%0A        List%3CInteger%3E rst = new ArrayList%3CInteger%3E();%0A        for (int i = 0; i %3C len; i++) %7B%0A            scannedMaxPos = Math.max(scannedMaxPos, last.get(S.charAt(i)));%0A            // when the index arrive the max pos of scanned letter%0A            if (i == scannedMaxPos) %7B%0A                rst.add(i - start + 1);%0A                start = i + 1;%0A            %7D%0A        %7D%0A        return rst;%0A    %7D%0A%7D%0A

Create PartitionLabels.java