forked from Dipak3007/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartitionLabels.cpp
More file actions
76 lines (67 loc) · 1.66 KB
/
PartitionLabels.cpp
File metadata and controls
76 lines (67 loc) · 1.66 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
Question : You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.
Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.
Return a list of integers representing the size of these parts.
Example :
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.
*/
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
// function to partition the string into labels
vector<int> partitionLabels (string str)
{
// creating unordered map
unordered_map <char, int> map;
int n = str.length();
// storing the last occurence index of characters into map
for (int i=0; i<n; i++)
{
map[str[i]] = i;
}
vector<int> v;
int prev = -1, getMax = 0;
for (int i=0; i<n; i++)
{
char ch = str.at(i);
getMax = max(getMax, map.at(ch));
if (getMax == i)
{
v.push_back( getMax - prev );
prev = getMax;
}
}
return v;
}
// driver method
int main ()
{
int t;
cout << "Enter the number of test cases : ";
cin >> t;
while (t--)
{
// taking string input from the user
string s;
cout << "Enter the string : ";
cin >> s;
// creating vector
vector<int> output;
output = partitionLabels (s);
// printing the output
for (int i=0; i<output.size(); i++)
{
cout << output[i] << " ";
}
cout << endl;
}
return 0;
}