Skip to content

Latest commit

 

History

History
42 lines (35 loc) · 1.03 KB

partition-labels.md

File metadata and controls

42 lines (35 loc) · 1.03 KB
/**
  Problem Name : Partition Labels
  Problem URL : https://leetcode.com/problems/partition-labels/
  Description :
    A string S of lowercase English 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.
    
  Difficulty : Medium
  Language : C#
  Category : Algorithms - Greedy
*/
public class Solution 
{
    public IList<int> PartitionLabels(string S) 
    {
        int[] last = new int[26];
        for(int i = 0; i < S.Length; i++)
            last[S[i] - 'a'] = i;
        
        List<int> parts = new List<int>();
        int partIndex = 0;
        int end = last[S[0] - 'a'];
        
        for(int i = 0; i < S.Length; i++)
        {
            end = Math.Max(end, last[S[i] - 'a']);
            
            if(i == end)
            {
                parts.Add(i - partIndex + 1);
                partIndex = i + 1;
            }
        }
        return parts;
    }
}