-
Notifications
You must be signed in to change notification settings - Fork 2
/
substring_problems.cpp
148 lines (111 loc) · 3.56 KB
/
substring_problems.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/****************************************************************************
File name: substring_problems.cpp
Author: babajr
*****************************************************************************/
/*
For most substring problem, we are given a string and need to find a substring of
it which satisfy some restrictions.
A general way is to use a hashmap assisted with two pointers. The template is given below.
*/
#include<bits/stdc++.h>
using namespace std;
/*
API to get the length of Longest Substring with At Most K Distinct Characters.
*/
int find_largest_substr_with_k_unique_char(char *str, int k)
{
int max_len = 0;
int window_start = 0;
int window_end = 0;
int counter = 0; // to keep the track of unique characters in the array.
int map[128] = {0}; // array to keep the count of characters from the input array.
while(window_end < strlen(str))
{
// increment counter if the char is distinct.
if(map[str[window_end]] == 0)
counter++;
// add the char at current index to the map and increment the index.
map[str[window_end]]++;
window_end++;
// do above only until distinct char are < k.
while(counter > k)
{
// decrement the counter for the unique character if found
if(map[str[window_start]] == 1)
counter--;
// shrink the sliding window:
// remove the char from the map array.
// also, increment the window_start.
map[str[window_start]]--;
window_start++;
}
max_len = max(max_len, window_end - window_start);
}
return max_len;
}
/*
API to get the length of Longest Substring with At Most 2 Distinct Characters.
Code and logic is same as above, just replace k with 2.
*/
int find_largest_substr_with_atmost_2_unique_char(char *str)
{
int max_len = 0;
int counter = 0;
int window_end = 0;
int window_start = 0;
int map[128] = {0};
while(window_end < strlen(str))
{
if(map[str[window_end]] == 0)
counter++;
map[str[window_end]]++;
window_end++;
while(counter > 2)
{
if(map[str[window_start]] == 1)
counter--;
map[str[window_start]]--;
window_start++;
}
max_len = max(max_len, window_end - window_start);
}
return max_len;
}
/*
API to get the length of Longest Substring Without Repeating Characters.
*/
int find_largest_substr_without_repeating_char(char *str)
{
int max_len = 0;
int counter = 0;
int window_end = 0;
int window_start = 0;
int map[128] = {0};
while(window_end < strlen(str))
{
if(map[str[window_end]] > 0)
counter++;
map[str[window_end]]++;
window_end++;
while(counter > 0)
{
if(map[str[window_start]] > 1)
counter--;
map[str[window_start]]--;
window_start++;
}
max_len = max(max_len, window_end - window_start);
}
return max_len;
}
int main(void)
{
char str[] = "aaaaracai";
cout << "Length of the longest substring with at most k distinct charactrers: "
<< find_largest_substr_with_k_unique_char(str, 3) << endl;
cout << "Length of the longest substring with at most k distinct charactrers: "
<< find_largest_substr_with_atmost_2_unique_char(str) << endl;
cout << "Length of the longest substring with at most k distinct charactrers: "
<< find_largest_substr_without_repeating_char(str) << endl;
return 0;
}