返回介绍

solution / 0700-0799 / 0763.Partition Labels / README_EN

发布于 2024-06-17 01:03:34 字数 5294 浏览 0 评论 0 收藏 0

763. Partition Labels

中文文档

Description

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.

Solutions

Solution 1

class Solution:
  def partitionLabels(self, s: str) -> List[int]:
    last = {c: i for i, c in enumerate(s)}
    mx = j = 0
    ans = []
    for i, c in enumerate(s):
      mx = max(mx, last[c])
      if mx == i:
        ans.append(i - j + 1)
        j = i + 1
    return ans
class Solution {
  public List<Integer> partitionLabels(String s) {
    int[] last = new int[26];
    int n = s.length();
    for (int i = 0; i < n; ++i) {
      last[s.charAt(i) - 'a'] = i;
    }
    List<Integer> ans = new ArrayList<>();
    int mx = 0, j = 0;
    for (int i = 0; i < n; ++i) {
      mx = Math.max(mx, last[s.charAt(i) - 'a']);
      if (mx == i) {
        ans.add(i - j + 1);
        j = i + 1;
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> partitionLabels(string s) {
    int last[26] = {0};
    int n = s.size();
    for (int i = 0; i < n; ++i) {
      last[s[i] - 'a'] = i;
    }
    vector<int> ans;
    int mx = 0, j = 0;
    for (int i = 0; i < n; ++i) {
      mx = max(mx, last[s[i] - 'a']);
      if (mx == i) {
        ans.push_back(i - j + 1);
        j = i + 1;
      }
    }
    return ans;
  }
};
func partitionLabels(s string) (ans []int) {
  last := [26]int{}
  for i, c := range s {
    last[c-'a'] = i
  }
  var mx, j int
  for i, c := range s {
    mx = max(mx, last[c-'a'])
    if mx == i {
      ans = append(ans, i-j+1)
      j = i + 1
    }
  }
  return
}
function partitionLabels(s: string): number[] {
  const last: number[] = Array(26).fill(0);
  const idx = (c: string) => c.charCodeAt(0) - 'a'.charCodeAt(0);
  const n = s.length;
  for (let i = 0; i < n; ++i) {
    last[idx(s[i])] = i;
  }
  const ans: number[] = [];
  for (let i = 0, j = 0, mx = 0; i < n; ++i) {
    mx = Math.max(mx, last[idx(s[i])]);
    if (mx === i) {
      ans.push(i - j + 1);
      j = i + 1;
    }
  }
  return ans;
}
impl Solution {
  pub fn partition_labels(s: String) -> Vec<i32> {
    let n = s.len();
    let bytes = s.as_bytes();
    let mut last = [0; 26];
    for i in 0..n {
      last[(bytes[i] - b'a') as usize] = i;
    }
    let mut ans = vec![];
    let mut j = 0;
    let mut mx = 0;
    for i in 0..n {
      mx = mx.max(last[(bytes[i] - b'a') as usize]);
      if mx == i {
        ans.push((i - j + 1) as i32);
        j = i + 1;
      }
    }
    ans
  }
}
/**
 * @param {string} s
 * @return {number[]}
 */
var partitionLabels = function (s) {
  const last = new Array(26).fill(0);
  const idx = c => c.charCodeAt() - 'a'.charCodeAt();
  const n = s.length;
  for (let i = 0; i < n; ++i) {
    last[idx(s[i])] = i;
  }
  const ans = [];
  for (let i = 0, j = 0, mx = 0; i < n; ++i) {
    mx = Math.max(mx, last[idx(s[i])]);
    if (mx === i) {
      ans.push(i - j + 1);
      j = i + 1;
    }
  }
  return ans;
};
public class Solution {
  public IList<int> PartitionLabels(string s) {
    int[] last = new int[26];
    int n = s.Length;
    for (int i = 0; i < n; i++) {
      last[s[i] - 'a'] = i;
    }
    IList<int> ans = new List<int>();
    for (int i = 0, j = 0, mx = 0; i < n; ++i) {
      mx = Math.Max(mx, last[s[i] - 'a']);
      if (mx == i) {
        ans.Add(i - j + 1);
        j = i + 1;
      }
    }
    return ans;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文