Skip to content

Instantly share code, notes, and snippets.

@rtsoy
Created January 3, 2026 06:18
Show Gist options
  • Select an option

  • Save rtsoy/405df91c68e91d545f5fb24f24971e4f to your computer and use it in GitHub Desktop.

Select an option

Save rtsoy/405df91c68e91d545f5fb24f24971e4f to your computer and use it in GitHub Desktop.
763. Partition Labels
// https://leetcode.com/problems/partition-labels/
//
// Time: O(n)
// Space: O(1)
//
// n = number of letters in string
// .................... //
func partitionLabels(s string) []int {
last := [26]int{}
for i := range s {
idx := s[i] - 'a'
last[idx] = i
}
res := make([]int, 0)
start, end := 0, 0
for i := range s {
li := s[i] - 'a'
if last[li] > end {
end = last[li]
}
if i == end {
res = append(res, end-start+1)
start = end + 1
}
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment