[LeetCode] 763. Partition Labels 分割标签

 

A string S of lowercase 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.html

 

Example 1:git

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.

 

Note:github

  1. S will have length in range [1, 500].
  2. S will consist of lowercase letters ('a' to 'z') only.

 

这道题给了咱们一个字符串S,然咱们将其尽量多的分割为子字符串,条件是每种字符最多只能出如今一个子串中。好比题目汇总的例子,字符串S中有多个a,这些a必须只能在第一个子串中,再好比全部的字母e值出如今了第二个子串中。那么这道题的难点就是如何找到字符串的断点,即拆分红为子串的位置。咱们仔细观察题目中的例子,能够发现一旦某个字母屡次出现了,那么其最后一个出现位置必需要在当前子串中,字母a,e,和j,分别是三个子串中的结束字母。因此咱们关注的是每一个字母最后的出现位置,咱们可使用一个 HashMap 来创建字母和其最后出现位置之间的映射,那么对于题目中的例子来讲,咱们能够获得以下映射:app

a -> 8
b -> 5
c -> 7
d -> 14
e -> 15
f -> 11
g -> 13
h -> 19
i -> 22
j -> 23
k -> 20
l -> 21less

创建好映射以后,就须要开始遍历字符串S了,咱们维护一个 start 变量,是当前子串的起始位置,还有一个 last 变量,是当前子串的结束位置,每当咱们遍历到一个字母,咱们须要在 HashMap 中提取出其最后一个位置,由于一旦当前子串包含了一个字母,其必须包含全部的相同字母,因此咱们要不停的用当前字母的最后一个位置来更新 last 变量,只有当i和 last 相同了,即当 i = 8 时,当前子串包含了全部已出现过的字母的最后一个位置,即以后的字符串里不会有以前出现过的字母了,此时就应该是断开的位置,咱们将长度9加入结果 res 中,同理类推,咱们能够找出以后的断开的位置,参见代码以下:post

 

class Solution {
public:
    vector<int> partitionLabels(string S) {
        vector<int> res;
        int n = S.size(), start = 0, last = 0;
        unordered_map<char, int> m;
        for (int i = 0; i < n; ++i) m[S[i]] = i;
        for (int i = 0; i < n; ++i) {
            last = max(last, m[S[i]]);
            if (i == last) {
                res.push_back(i - start + 1);
                start = i + 1;
            }
        }
        return res;
    }
};

 

Github 同步地址:this

https://github.com/grandyang/leetcode/issues/763url

 

相似题目:spa

Merge Intervalscode

 

参考资料:

https://leetcode.com/problems/partition-labels/

https://leetcode.com/problems/partition-labels/discuss/113259/Java-2-pass-O(n)-time-O(1)-space-extending-end-pointer-solution

 

LeetCode All in One 题目讲解汇总(持续更新中...)

相关文章
相关标签/搜索