Created
January 3, 2026 06:18
-
-
Save rtsoy/405df91c68e91d545f5fb24f24971e4f to your computer and use it in GitHub Desktop.
763. Partition Labels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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