---
# System prepended metadata

title: 763. Partition Labels
tags: [Two Pointer]

---

# 763. Partition Labels
###### tags: `Two Pointer`

[leetcode link](https://leetcode.com/problems/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.

**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.
```

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

**Solution:**
```java
class Solution {
    public List<Integer> partitionLabels(String S) {
        
        // init vars: hashmap
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        List<Integer> resList = new ArrayList<Integer>();
        int n = S.length;
        
        // save index to map
        for(int i=0; i<n; i++){
            map.put(S.charAt(i), i);
        }
        
        // iterate the input str -> check index in the hashmap
        // -> if its right boundry -> add it to res
        
        int anchor = 0;
        int bound = 0;
        for(int i=0; i<n; i++){
            bound = Math.max(bound, map.get(S.charAt(i)));
            if(bound == i)){
                resList.add(i-anchor+1);
                anchor = i + 1;
            }
        }
        
        // return res col
        return resList;
    }
}
```