From 3ecb787e9224f30df33a20bd37f22fb6e07d2abf Mon Sep 17 00:00:00 2001 From: Silkie Agarwal Date: Thu, 2 Oct 2025 14:46:01 +0530 Subject: [PATCH 1/6] added code for longest-substring-without-repeating-characters in cpp --- .vscode/tasks.json | 29 +++++ ...substring-without-repeating-characters.cpp | 112 ++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 .vscode/tasks.json create mode 100644 CPP/Sliding window/longest-substring-without-repeating-characters.cpp diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..4ede37a --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: clang build active file", + "command": "/usr/bin/clang", + "args": [ + "-fcolor-diagnostics", + "-fansi-escape-codes", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp new file mode 100644 index 0000000..9afd50a --- /dev/null +++ b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp @@ -0,0 +1,112 @@ +#include +using namespace std; +/** + * Problem: Longest Substring Without Repeating Characters + * + * This solution uses the "Sliding Window" technique along with an + * unordered map (hash map) to efficiently find the length of the + * longest substring without repeating characters. + */ +class Solution { +public: + /** + * Finds the length of the longest substring without repeating characters. + * + * @param s The input string. + * @return The length of the longest substring without repeating characters. + */ + int lengthOfLongestSubstring(string s) { + // Handle the edge case for an empty string. + if (s.size() == 0) return 0; + + // 'mp' will store the frequency of characters in the current window (i to j). + // Key: character (char), Value: count (int). + unordered_map mp; + + // 'i' is the start (left) of the window, 'j' is the end (right) of the window. + int i = 0, j = 0; + // 'ans' stores the maximum length found so far. + int ans = 0; + + // Iterate through the string, expanding the window with 'j'. + while (j < s.size()) { + // 1. Expand the window to the right: Add the new character s[j]. + mp[s[j]]++; + + // Check the current window: The substring from s[i] to s[j] is j-i+1 characters long. + + // 2. Case 1: All characters in the window are unique. + // If the number of unique characters (mp.size()) is equal to the window size (j-i+1), + // we have a valid non-repeating substring. Update the max length. + if (mp.size() == j - i + 1) { + ans = std::max(ans, j - i + 1); + } + // 3. Case 2: There are repeating characters in the window. + // If mp.size() is less than j-i+1, at least one character is repeated. + else if (mp.size() < j - i + 1) { + // Shrink the window from the left by moving 'i' until the repetition is gone. + while (mp.size() < j - i + 1) { + // Remove the character at the left end of the window (s[i]). + mp[s[i]]--; + + // If the count of s[i] drops to 0, it's no longer present in the window. + // Remove it from the map to maintain the accurate unique character count (mp.size()). + if (mp[s[i]] == 0) { + mp.erase(s[i]); + } + + // Move the left pointer 'i' to the right to shrink the window. + i++; + } + // Note: After shrinking, the window is valid again, but we don't update 'ans' here + // because a smaller valid window can't be the longest. The expansion step (j++) + // will eventually lead to a new max length. + } + // Move the right pointer 'j' to expand the window again in the next iteration. + j++; + } + + // Return the final maximum length found. + return ans; + } +}; + +// --- +// ## Main Function for Testing +// --- + +int main() { + Solution sol; + + // Test Case 1: Simple longest substring + std::string s1 = "abcabcbb"; + int result1 = sol.lengthOfLongestSubstring(s1); + std::cout << "Input: \"" << s1 << "\" | Longest Substring Length: " << result1 << " (Expected: 3)\n"; + + // Test Case 2: All repeating characters + std::string s2 = "bbbbb"; + int result2 = sol.lengthOfLongestSubstring(s2); + std::cout << "Input: \"" << s2 << "\" | Longest Substring Length: " << result2 << " (Expected: 1)\n"; + + // Test Case 3: Longest substring at the end + std::string s3 = "pwwkew"; + int result3 = sol.lengthOfLongestSubstring(s3); + std::cout << "Input: \"" << s3 << "\" | Longest Substring Length: " << result3 << " (Expected: 3)\n"; + + // Test Case 4: Empty string + std::string s4 = ""; + int result4 = sol.lengthOfLongestSubstring(s4); + std::cout << "Input: \"" << s4 << "\" | Longest Substring Length: " << result4 << " (Expected: 0)\n"; + + // Test Case 5: Complex case + std::string s5 = "dvdf"; + int result5 = sol.lengthOfLongestSubstring(s5); + std::cout << "Input: \"" << s5 << "\" | Longest Substring Length: " << result5 << " (Expected: 3)\n"; + + // Test Case 6: Longest possible + std::string s6 = "abcdefgh"; + int result6 = sol.lengthOfLongestSubstring(s6); + std::cout << "Input: \"" << s6 << "\" | Longest Substring Length: " << result6 << " (Expected: 8)\n"; + + return 0; +} \ No newline at end of file From 34e4f77bc2ca231fa681f87f432f696142735dcc Mon Sep 17 00:00:00 2001 From: Silkie Agarwal Date: Thu, 2 Oct 2025 15:11:04 +0530 Subject: [PATCH 2/6] added code for longest-substring-without-repeating-characters in cpp --- .vscode/tasks.json | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 .vscode/tasks.json diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 4ede37a..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: clang build active file", - "command": "/usr/bin/clang", - "args": [ - "-fcolor-diagnostics", - "-fansi-escape-codes", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} \ No newline at end of file From 2ea12d5f3a3dc2f6a79f0fee49f5a3a7147131be Mon Sep 17 00:00:00 2001 From: Silkie Agarwal Date: Thu, 2 Oct 2025 15:13:51 +0530 Subject: [PATCH 3/6] added code for longest-substring-without-repeating-characters in cpp --- ...substring-without-repeating-characters.cpp | 121 ++++-------------- 1 file changed, 23 insertions(+), 98 deletions(-) diff --git a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp index 9afd50a..dda884f 100644 --- a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp +++ b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp @@ -7,106 +7,31 @@ using namespace std; * unordered map (hash map) to efficiently find the length of the * longest substring without repeating characters. */ -class Solution { -public: - /** - * Finds the length of the longest substring without repeating characters. - * - * @param s The input string. - * @return The length of the longest substring without repeating characters. - */ - int lengthOfLongestSubstring(string s) { - // Handle the edge case for an empty string. - if (s.size() == 0) return 0; - // 'mp' will store the frequency of characters in the current window (i to j). - // Key: character (char), Value: count (int). - unordered_map mp; - - // 'i' is the start (left) of the window, 'j' is the end (right) of the window. - int i = 0, j = 0; - // 'ans' stores the maximum length found so far. - int ans = 0; - - // Iterate through the string, expanding the window with 'j'. - while (j < s.size()) { - // 1. Expand the window to the right: Add the new character s[j]. - mp[s[j]]++; - - // Check the current window: The substring from s[i] to s[j] is j-i+1 characters long. - - // 2. Case 1: All characters in the window are unique. - // If the number of unique characters (mp.size()) is equal to the window size (j-i+1), - // we have a valid non-repeating substring. Update the max length. - if (mp.size() == j - i + 1) { - ans = std::max(ans, j - i + 1); - } - // 3. Case 2: There are repeating characters in the window. - // If mp.size() is less than j-i+1, at least one character is repeated. - else if (mp.size() < j - i + 1) { - // Shrink the window from the left by moving 'i' until the repetition is gone. - while (mp.size() < j - i + 1) { - // Remove the character at the left end of the window (s[i]). - mp[s[i]]--; - - // If the count of s[i] drops to 0, it's no longer present in the window. - // Remove it from the map to maintain the accurate unique character count (mp.size()). - if (mp[s[i]] == 0) { - mp.erase(s[i]); - } - - // Move the left pointer 'i' to the right to shrink the window. - i++; - } - // Note: After shrinking, the window is valid again, but we don't update 'ans' here - // because a smaller valid window can't be the longest. The expansion step (j++) - // will eventually lead to a new max length. - } - // Move the right pointer 'j' to expand the window again in the next iteration. - j++; - } - - // Return the final maximum length found. - return ans; +int solve(string str) { + + if(str.size()==0) + return 0; + int maxans = INT_MIN; + for (int i = 0; i < str.length(); i++) // outer loop for traversing the string + { + unordered_set < int > set; + for (int j = i; j < str.length(); j++) // nested loop for getting different string starting with str[i] + { + if (set.find(str[j]) != set.end()) // if element if found so mark it as ans and break from the loop + { + maxans = max(maxans, j - i); + break; + } + set.insert(str[j]); } -}; - -// --- -// ## Main Function for Testing -// --- + } + return maxans; +} int main() { - Solution sol; - - // Test Case 1: Simple longest substring - std::string s1 = "abcabcbb"; - int result1 = sol.lengthOfLongestSubstring(s1); - std::cout << "Input: \"" << s1 << "\" | Longest Substring Length: " << result1 << " (Expected: 3)\n"; - - // Test Case 2: All repeating characters - std::string s2 = "bbbbb"; - int result2 = sol.lengthOfLongestSubstring(s2); - std::cout << "Input: \"" << s2 << "\" | Longest Substring Length: " << result2 << " (Expected: 1)\n"; - - // Test Case 3: Longest substring at the end - std::string s3 = "pwwkew"; - int result3 = sol.lengthOfLongestSubstring(s3); - std::cout << "Input: \"" << s3 << "\" | Longest Substring Length: " << result3 << " (Expected: 3)\n"; - - // Test Case 4: Empty string - std::string s4 = ""; - int result4 = sol.lengthOfLongestSubstring(s4); - std::cout << "Input: \"" << s4 << "\" | Longest Substring Length: " << result4 << " (Expected: 0)\n"; - - // Test Case 5: Complex case - std::string s5 = "dvdf"; - int result5 = sol.lengthOfLongestSubstring(s5); - std::cout << "Input: \"" << s5 << "\" | Longest Substring Length: " << result5 << " (Expected: 3)\n"; - - // Test Case 6: Longest possible - std::string s6 = "abcdefgh"; - int result6 = sol.lengthOfLongestSubstring(s6); - std::cout << "Input: \"" << s6 << "\" | Longest Substring Length: " << result6 << " (Expected: 8)\n"; - - return 0; + string str = "takeUforward"; + cout << "The length of the longest substring without repeating characters is " << + solve(str); + return 0; } \ No newline at end of file From b180e0a8658f310a6e0ce9e3e14f14b71382e9ef Mon Sep 17 00:00:00 2001 From: Silkie Agarwal Date: Thu, 2 Oct 2025 15:16:01 +0530 Subject: [PATCH 4/6] added code for longest-substring-without-repeating-characters in cpp --- .../longest-substring-without-repeating-characters.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp index dda884f..9d1f5f9 100644 --- a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp +++ b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp @@ -1,11 +1,7 @@ #include using namespace std; /** - * Problem: Longest Substring Without Repeating Characters - * - * This solution uses the "Sliding Window" technique along with an - * unordered map (hash map) to efficiently find the length of the - * longest substring without repeating characters. +Longest Substring Without Repeating Characters */ int solve(string str) { From 7a77b63fa9bc557ffbf02cf30cc5680c55653ec4 Mon Sep 17 00:00:00 2001 From: Silkie Agarwal Date: Thu, 2 Oct 2025 15:18:09 +0530 Subject: [PATCH 5/6] added code for longest-substring-without-repeating-characters in cpp --- .../longest-substring-without-repeating-characters.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp index 9d1f5f9..4288769 100644 --- a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp +++ b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp @@ -1,8 +1,6 @@ +// Problem: Longest Substring Without Repeating Characters #include using namespace std; -/** -Longest Substring Without Repeating Characters - */ int solve(string str) { From d6b2aed52a67cbeda8c2762fe2b264834c062aaa Mon Sep 17 00:00:00 2001 From: Silkie Agarwal Date: Thu, 2 Oct 2025 15:19:56 +0530 Subject: [PATCH 6/6] added code for longest-substring-without-repeating-characters in cpp --- ...substring-without-repeating-characters.cpp | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp index 4288769..cccd3c9 100644 --- a/CPP/Sliding window/longest-substring-without-repeating-characters.cpp +++ b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp @@ -1,31 +1,30 @@ // Problem: Longest Substring Without Repeating Characters #include using namespace std; +class Solution { + public: + int lengthofLongestSubstring(string s) { + vector < int > mpp(256, -1); -int solve(string str) { + int left = 0, right = 0; + int n = s.size(); + int len = 0; + while (right < n) { + if (mpp[s[right]] != -1) + left = max(mpp[s[right]] + 1, left); - if(str.size()==0) - return 0; - int maxans = INT_MIN; - for (int i = 0; i < str.length(); i++) // outer loop for traversing the string - { - unordered_set < int > set; - for (int j = i; j < str.length(); j++) // nested loop for getting different string starting with str[i] - { - if (set.find(str[j]) != set.end()) // if element if found so mark it as ans and break from the loop - { - maxans = max(maxans, j - i); - break; + mpp[s[right]] = right; + + len = max(len, right - left + 1); + right++; } - set.insert(str[j]); + return len; } - } - return maxans; -} +}; int main() { string str = "takeUforward"; - cout << "The length of the longest substring without repeating characters is " << - solve(str); + Solution obj; + cout << "The length of the longest substring without repeating characters is " << obj.lengthofLongestSubstring(str); return 0; } \ No newline at end of file