-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd7.cpp
111 lines (102 loc) · 3.03 KB
/
d7.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
// general format for variable size sliding window problem
/*
while(j<size){calculations}
if(condition<k){j++}
elseif(condition==K){ans<-calculation;j++}
elseif(current>k){
while(current>k){remove calculation for i(currentWindowIndex);i++}
}
*/
// Longest substring with non-repeating characters
// answer
// My initial idea to solve this problem is to use map and pair along with the given string to solve.
// so each unique character would be paired with its index
// and map[c] would take care of the occurences of the specified character
// whenever map[c]++ makes map[c] == 2 we would return use maxLength = max(maxLength,i-currentWindowIndex+1)
// after this is where pair shines - we would get the pair of c from before and set the currentWindowIndex prior to it and continue the loop
// then decrement map[c]
// also remove that pair and create new pair
// return maxLength
#include <iostream>
#include <climits>
#include <string.h>
#include <unordered_map>
using namespace std;
int initialAnswer(string s)
{
int currentWindowIndex;
unordered_map<char, int> map;
pair<char, int> pr;
int maxLength = INT_MIN;
int n = s.length();
for (int i = 0; i < n; i++)
{
map[s[i]]++;
if (map[s[i]] == 2)
{
maxLength = max(maxLength, i - currentWindowIndex + 1);
// currentWindowIndex = //use pair to update the window index to the next element from s[i]
// update pair[s[i]] with i thereby removing the previous second value of s[i]
}
}
return maxLength;
}
// it can be solved without using pair by using map.find and just storing the index in map
// gpt reviewed
int initialAnswer(string s)
{
int currentWindowIndex = 0;
unordered_map<char, int> map;
int maxLength = INT_MIN;
int n = s.length();
for (int i = 0; i < n; i++)
{
if (map.find(s[i]) != map.end())
{
currentWindowIndex = max(currentWindowIndex, map[s[i]] + 1);
}
maxLength = max(maxLength, i - currentWindowIndex + 1);
map[s[i]] = i;
}
return maxLength;
}
// longest substring with K unique characters
int answer1(string s)
{
int n = s.length();
unordered_map<char, int> map;
int maxSubstring = 0;
int currentWindowIndex = 0;
for (int i = 0; i < n; i++)
{
if (map.find(s[i]) != map.end())
{
maxSubstring = max(maxSubstring, i - currentWindowIndex + 1);
currentWindowIndex = max(currentWindowIndex, 1 + map[s[i]]);
}
map[s[i]] = i;
}
return maxSubstring;
}
/// longest substring with k unique characters
int uni(string s)
{
unordered_map<char, int> charMap;
int n = s.size();
int windowSize = 0;
int maxWindow = 0;
for (int i = 0; i < n; i++)
{
if (charMap.find(s[i]) != charMap.end())
{
charMap[s[i]] = i;
}
else
{
maxWindow = max(maxWindow, i - windowSize);
windowSize = charMap[s[i]] + 1;
charMap[s[i]] = i;
}
}
return maxWindow;
}