Skip to content

Commit e97ae5b

Browse files
Merge pull request #596 from DeveloperViraj/feat/day-02-strings
feat(revision): add Day 02 - Strings (Longest Substring, Minimum Window)
2 parents ba2fe54 + ab6ec5b commit e97ae5b

File tree

6 files changed

+153
-0
lines changed

6 files changed

+153
-0
lines changed
File renamed without changes.

CPP/revision/25-revision-questions/day-01-two-pointers-hashing/01_3sum.cpp renamed to CPP/algorithms/revision/25-revision-questions/day-01-two-pointers-hashing/01_3sum.cpp

File renamed without changes.

CPP/revision/25-revision-questions/day-01-two-pointers-hashing/02_container_with_most_water.cpp renamed to CPP/algorithms/revision/25-revision-questions/day-01-two-pointers-hashing/02_container_with_most_water.cpp

File renamed without changes.

CPP/revision/25-revision-questions/day-01-two-pointers-hashing/03_longest_consecutive_sequence.cpp renamed to CPP/algorithms/revision/25-revision-questions/day-01-two-pointers-hashing/03_longest_consecutive_sequence.cpp

File renamed without changes.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// 01_longest_substring_without_repeating_characters.cpp
2+
//
3+
// 🔍 Algorithm: Longest Substring Without Repeating Characters
4+
// Description:
5+
// Finds the length of the longest substring in a given string
6+
// that contains no repeating characters.
7+
// Uses the Sliding Window technique with a hash map to track
8+
// character indices and adjust window boundaries efficiently.
9+
//
10+
// Use Case:
11+
// Commonly used to analyze substring patterns or find unique
12+
// character sequences in data streams.
13+
//
14+
// Time Complexity: O(n) — each character visited once.
15+
// Space Complexity: O(1) — constant size (at most 256 chars).
16+
//
17+
// LeetCode: https://leetcode.com/problems/longest-substring-without-repeating-characters/
18+
#include <iostream>
19+
#include <string>
20+
#include <unordered_map>
21+
using namespace std;
22+
23+
int lengthOfLongestUniqueSubstring(const string &s) {
24+
unordered_map<char,int> lastSeen;
25+
int maxLength = 0;
26+
int windowStart = 0;
27+
28+
for (int windowEnd = 0; windowEnd < (int)s.size(); ++windowEnd) {
29+
char currentChar = s[windowEnd];
30+
if (lastSeen.find(currentChar) != lastSeen.end()) {
31+
// If this character appeared inside current window, move start
32+
if (lastSeen[currentChar] >= windowStart) {
33+
windowStart = lastSeen[currentChar] + 1;
34+
}
35+
}
36+
lastSeen[currentChar] = windowEnd;
37+
int windowLength = windowEnd - windowStart + 1;
38+
if (windowLength > maxLength) maxLength = windowLength;
39+
}
40+
return maxLength;
41+
}
42+
43+
int main() {
44+
cout << "Longest Substring Without Repeating Characters\n";
45+
cout << "Enter a single-line string and press Enter:\n";
46+
string input;
47+
if (!getline(cin, input)) return 0;
48+
49+
int result = lengthOfLongestUniqueSubstring(input);
50+
cout << "Length: " << result << "\n";
51+
return 0;
52+
}
53+
54+
/*
55+
Example:
56+
Input:
57+
abcabcbb
58+
Output:
59+
Length: 3 (substring "abc")
60+
*/
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// 02_minimum_window_substring.cpp
2+
//
3+
// 🔍 Algorithm: Minimum Window Substring
4+
// Description:
5+
// Finds the smallest substring in `source` that contains all
6+
// characters of `target` (including duplicates).
7+
// Uses a Sliding Window approach combined with frequency maps
8+
// to expand and contract the window optimally.
9+
//
10+
// Use Case:
11+
// Useful in text search problems, DNA sequence matching,
12+
// or when finding minimal segments containing specific data.
13+
//
14+
// Time Complexity: O(|S| + |T|)
15+
// Space Complexity: O(|T|)
16+
//
17+
// LeetCode: https://leetcode.com/problems/minimum-window-substring/
18+
19+
20+
#include <iostream>
21+
#include <string>
22+
#include <unordered_map>
23+
#include <climits>
24+
using namespace std;
25+
26+
string minWindow(const string &source, const string &target) {
27+
if (target.empty() || source.empty() || source.size() < target.size()) return "";
28+
29+
unordered_map<char,int> need;
30+
for (char c : target) need[c]++;
31+
32+
int required = need.size(); // distinct characters needed
33+
int formed = 0; // how many distinct characters currently satisfy required count
34+
unordered_map<char,int> windowCounts;
35+
36+
int left = 0, right = 0;
37+
int bestLeft = 0;
38+
int bestLen = INT_MAX;
39+
40+
while (right < (int)source.size()) {
41+
char c = source[right];
42+
windowCounts[c]++;
43+
44+
// If current char count matches the required count, we've satisfied one distinct char
45+
if (need.count(c) && windowCounts[c] == need[c]) {
46+
formed++;
47+
}
48+
49+
// When all required chars are satisfied, try to shrink from the left
50+
while (left <= right && formed == required) {
51+
if (right - left + 1 < bestLen) {
52+
bestLen = right - left + 1;
53+
bestLeft = left;
54+
}
55+
56+
char leftChar = source[left];
57+
windowCounts[leftChar]--;
58+
if (need.count(leftChar) && windowCounts[leftChar] < need[leftChar]) {
59+
formed--;
60+
}
61+
left++;
62+
}
63+
64+
right++;
65+
}
66+
67+
if (bestLen == INT_MAX) return "";
68+
return source.substr(bestLeft, bestLen);
69+
}
70+
71+
int main() {
72+
cout << "Minimum Window Substring\n";
73+
cout << "Input format (two lines):\nS\nT\n\nEnter S (source string) and T (target string) each on a new line:\n";
74+
75+
string S, T;
76+
if (!getline(cin, S)) return 0;
77+
if (!getline(cin, T)) return 0;
78+
79+
string answer = minWindow(S, T);
80+
if (answer.empty()) {
81+
cout << "No valid window found\n";
82+
} else {
83+
cout << "Minimum window: " << answer << "\n";
84+
}
85+
return 0;
86+
}
87+
88+
/*
89+
Example:
90+
S = "ADOBECODEBANC"
91+
T = "ABC"
92+
Output: "BANC"
93+
*/

0 commit comments

Comments
 (0)