Created
April 22, 2018 07:57
-
-
Save satveersm/7b7bf2a5d826ebeecbdf71889e5fcfd3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MakeCoinChange.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
#include <array> | |
using namespace std; | |
vector<int> SplitsSentense(string s) | |
{ | |
vector<int> width_string_parts; | |
int length = s.length(); | |
int arr[26] = {-1}; | |
for (int index = length - 1; index >= 0; --index) | |
{ | |
if (arr[s[index] - 'a'] == -1) | |
{ | |
arr[s[index] - 'a'] = index; //Last occurance of this char found at some index x | |
} | |
} | |
int part_start = 0; | |
int index = 0; | |
int prev_depend_index = -1; | |
while (index < length) | |
{ | |
//is this is last occurance of this char | |
if (arr[s[index] - 'a'] <= index) //it cna be a split point | |
{ | |
if (prev_depend_index <= index) | |
{ | |
width_string_parts.push_back(index - part_start + 1); | |
prev_depend_index = -1; | |
part_start = index + 1; | |
} | |
} | |
if (prev_depend_index < arr[s[index] - 'a']) | |
{ | |
prev_depend_index = arr[s[index] - 'a']; | |
} | |
index++; | |
} | |
return width_string_parts; | |
} | |
int main() | |
{ | |
string s("ababcbacadefegdehijhklij"); | |
vector<int> v = SplitsSentense(s); | |
for(auto x : v) | |
{ | |
cout << x << " "; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment