forked from mohitjain/leetcode_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path763_partition_labels.rb
More file actions
50 lines (44 loc) · 1.38 KB
/
763_partition_labels.rb
File metadata and controls
50 lines (44 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Leetcode Problem: https://leetcode.com/problems/partition-labels/
#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.
#
#
#
# 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 English letters ('a' to 'z') only.
# ----------------------------------------------------------------------------------------------------------------------
# @param {String} s
# @return {Integer[]}
def partition_labels(s)
parition_lengths = []
last_indexes = {}
s.split('').each_with_index do |char, index|
last_indexes[char] = index
end
index = 0
while index < s.length
char = s[index]
end_index = last_indexes[char]
j = index
while j < end_index
end_index = [end_index, last_indexes[s[j]]].max
j += 1
end
parition_lengths.push j - index + 1
index = j + 1
end
parition_lengths
end
p partition_labels('ababcbacadefegdehijhklij')