-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path2405-optimal-partition-of-string.rb
59 lines (47 loc) · 1.32 KB
/
2405-optimal-partition-of-string.rb
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
51
52
53
54
55
56
57
58
59
# frozen_string_literal: true
# 2405. Optimal Partition of String
# https://leetcode.com/problems/optimal-partition-of-string
=begin
Given a string s, partition the string into one or more substrings such that the characters in each substring are unique. That is, no letter appears in a single substring more than once.
Return the minimum number of substrings in such a partition.
Note that each character should belong to exactly one substring in a partition.
### Example 1:
Input: s = "abacaba"
Output: 4
Explanation:
Two possible partitions are ("a","ba","cab","a") and ("ab","a","ca","ba").
It can be shown that 4 is the minimum number of substrings needed.
### Example 2:
Input: s = "ssssss"
Output: 6
Explanation:
The only valid partition is ("s","s","s","s","s","s").
### Constraints:
* 1 <= s.length <= 105
* s consists of only English lowercase letters.
=end
# @param {String} s
# @return {Integer}
def partition_string(s)
temp = ""
count = 1
s.each_char do |c|
if temp.include?(c)
count += 1
temp = c
else
temp += c
end
end
count
end
# ********************#
# TEST #
# ********************#
require "test/unit"
class Test_partition_string < Test::Unit::TestCase
def test_
assert_equal 4, partition_string("abacaba")
assert_equal 6, partition_string("ssssss")
end
end