763. Partition Labels

key is

if (i == maxPartIndex) { // add to res,

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.

因為要 as many parts as possible parts, 所以要 greedy 的當有機會可以分 part 就分, 不然 part 只會變少, 所以當 index == maxPartIndex (part 最大的 index)時, 就可以分了, 不然part 只會更長

O(n), O(1)

Last updated

Was this helpful?