JIAKAOBO

LeetCode

venmo
wechat

感谢赞助!

  • ㊗️
  • 大家
  • offer
  • 多多!

Problem

You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.

Return 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 that each letter appears in at most one part.
A partition like "ababcbacadefegde", "hijhklij" is incorrect, because it splits s into less parts.

Example 2:

Input: s = "eccbbbbdec"
Output: [10]

Constraints:

  • 1 <= s.length <= 500
  • s consists of lowercase English letters.

Code

class Solution {
    public List<Integer> partitionLabels(String s) {
        int[] last = new int[26];
        for (int i = 0; i < s.length(); ++i) {
            last[s.charAt(i) - 'a'] = i;
        }

        // 如果当前字符串中有这个字符,那么至少也要到这个index才能包括所有已存在的字符
        int minIndex = 0;
        int start = 0;
        List<Integer> res = new ArrayList();

        for (int i = 0; i < s.length(); ++i) {
            minIndex = Math.max(minIndex, last[s.charAt(i) - 'a']);
            if (i == minIndex) {
                res.add(i - start + 1);
                start = i + 1;
            }
        }
        return res;
    }
}
class Solution {
    public List<Integer> partitionLabels(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        HashMap<Integer, String> acc = new HashMap<>();
        int[] visit = new int[26];

        for (int i = 0; i < s.length(); i++) {
            map.put(s.charAt(i), i);
            visit[s.charAt(i) - 'a']++;
            acc.put(i, helper(visit));
        }

        PriorityQueue<Map.Entry<Character, Integer>> queue = new PriorityQueue<>((a, b) -> {
            return a.getValue() - b.getValue();
        });

        for (Map.Entry<Character, Integer> entry : map.entrySet()) {
            queue.offer(entry);
        }


        int[] lastAcc = new int[26];
        List<Integer> res = new ArrayList<>();
        int start = 0;
        while (!queue.isEmpty()) {
            Map.Entry<Character, Integer> entry = queue.poll();
            char c = entry.getKey();
            lastAcc[c - 'a']++;
            int lastIndex = entry.getValue();
            String accStr = acc.get(lastIndex);
            String lastAccStr = helper(lastAcc);

            if (accStr.equals(lastAccStr)) {
                res.add(lastIndex - start + 1);
                start = lastIndex + 1;
            }
        }

        return res;
    }

    private String helper(int[] visit) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < visit.length; i++) {
            if (visit[i] != 0) {
                sb.append('a' + i);
            }
        }

        return sb.toString();
    }
}