-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob3.cpp
More file actions
105 lines (98 loc) · 2.48 KB
/
prob3.cpp
File metadata and controls
105 lines (98 loc) · 2.48 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
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
/*
Medium - Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description
*/
/*
Solution 1:
Using a sliding window approach.
Key Steps:
Uses an unordered map lastSeen to track the last position of each character in the string.
Iterates through the string with two pointers, i and j, which define the window of the substring.
Adjusts the j pointer if a repeating character is found within the window to ensure no repetition.
Updates the maxlen variable to track the length of the longest substring.
Efficiency: Runs in O(n) time due to the sliding window and hash map operations.
*/
using namespace std;
#include <unordered_map>
#include <string>
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
unordered_map<char, int> lastSeen;
int maxlen = 0;
for (int i = 0, j = 0; i < s.length(); i++)
{
if (lastSeen.find(s[i]) != lastSeen.end() && lastSeen[s[i]] >= j)
j = lastSeen[s[i]] + 1;
lastSeen[s[i]] = i;
if (i - j + 1 > maxlen)
maxlen = i - j + 1;
}
return maxlen;
}
};
/*
Solution 2:
A more explicit string manipulation approach.
Key Steps:
Defines helper functions sub_string (to extract a substring) and is_present (to check if a character is in a substring).
Maintains a substring new_s to track characters in the current window.
Iterates through the input string with two indices, i and j, adjusting them based on character repetition.
Updates the size variable to track the longest substring length using the max_int helper function.
Efficiency: Runs in O(n^2) time due to nested loops and repeated substring manipulations.
*/
class Solution
{
public:
string sub_string(string s, int i, int j)
{
string news = "";
for (int z = i; z <= j; z++)
news += s[z];
return news;
}
bool is_present(string s, char c)
{
for (int i = 0; i < s.size(); i++)
if (s[i] == c)
return true;
return false;
}
int max_int(int x, int y) { return (x >= y) ? x : y; }
int lengthOfLongestSubstring(string s)
{
if (s.size() == 0)
return 0;
if (s.size() == 1)
return 1;
string new_s = "";
new_s += s[0];
int size = 1, i = 0, j = 0;
while (j != s.size() - 1)
{
if (!is_present(new_s, s[j + 1]))
{
new_s += s[j + 1];
j++;
size = max_int(size, j - i + 1);
}
else
{
if (j - i == 0)
{
j++;
i++;
new_s = sub_string(s, i, j);
}
else
{
i++;
new_s = sub_string(s, i, j);
}
}
}
return size;
}
};