From 8529473c0cbcdb9ccfca21e3b3f8f661b5ed70ed Mon Sep 17 00:00:00 2001 From: H Rohitha Aiswarya Date: Thu, 31 Oct 2024 21:49:45 +0530 Subject: [PATCH 1/2] Create Longest_Substring_Without_Repeating_Characters.cpp Added a new solution file Longest_Substring_Without_Repeating_Characters.cpp, which implements a C++ program to find the length of the longest substring without repeating characters in a given string. This program uses a sliding window approach along with a hash map to store the last seen index of each character, allowing for efficient tracking of unique substrings. The code takes user input for the string and outputs the maximum length of any substring without duplicate characters. Comments and an example are included for clarity. --- ...Substring_Without_Repeating_Characters.cpp | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp diff --git a/Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp b/Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp new file mode 100644 index 0000000..fa09d19 --- /dev/null +++ b/Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +using namespace std; + +// Function to find the length of the longest substring without repeating characters +int lengthOfLongestSubstring(const string &s) { + unordered_map charIndexMap; + int maxLength = 0; + int start = 0; + + for (int end = 0; end < s.length(); end++) { + char currentChar = s[end]; + + // If the character is already in the map and is within the current window + if (charIndexMap.find(currentChar) != charIndexMap.end() && charIndexMap[currentChar] >= start) { + start = charIndexMap[currentChar] + 1; + } + + // Update the last seen index of the current character + charIndexMap[currentChar] = end; + + // Calculate the max length of substring + maxLength = max(maxLength, end - start + 1); + } + return maxLength; +} + +int main() { + string input; + cout << "Enter a string: "; + cin >> input; + + int result = lengthOfLongestSubstring(input); + cout << "The length of the longest substring without repeating characters is: " << result << endl; + + return 0; +} + +/* +Example: +Input: "abcabcbb" +Explanation: +- The longest substring without repeating characters is "abc", which has a length of 3. +- As we iterate through the string: + - Start with "a" -> length 1 + - Add "b" -> "ab" -> length 2 + - Add "c" -> "abc" -> length 3 + - "a" repeats, so move the start to the next character -> "bca" (length still 3) + - Continue until the end, with the longest substring remaining "abc". +Output: 3 + +Input: "bbbbb" +Explanation: +- The longest substring without repeating characters is "b", which has a length of 1. +Output: 1 +*/ From 7c024897671d2a37b254af90ebdac0d3eff23fb0 Mon Sep 17 00:00:00 2001 From: H Rohitha Aiswarya Date: Fri, 1 Nov 2024 10:45:47 +0530 Subject: [PATCH 2/2] Update Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp Co-authored-by: Shamith --- ...Substring_Without_Repeating_Characters.cpp | 66 ++++++++++--------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp b/Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp index fa09d19..efc5b4b 100644 --- a/Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp +++ b/Solved-Problems/Longest_Substring_Without_Repeating_Characters/Longest_Substring_Without_Repeating_Characters.cpp @@ -1,57 +1,63 @@ #include -#include #include +#include using namespace std; -// Function to find the length of the longest substring without repeating characters +// Function to find the length of the longest substring without repeating +// characters int lengthOfLongestSubstring(const string &s) { - unordered_map charIndexMap; - int maxLength = 0; - int start = 0; - - for (int end = 0; end < s.length(); end++) { - char currentChar = s[end]; - - // If the character is already in the map and is within the current window - if (charIndexMap.find(currentChar) != charIndexMap.end() && charIndexMap[currentChar] >= start) { - start = charIndexMap[currentChar] + 1; - } - - // Update the last seen index of the current character - charIndexMap[currentChar] = end; - - // Calculate the max length of substring - maxLength = max(maxLength, end - start + 1); + unordered_map charIndexMap; + int maxLength = 0; + int start = 0; + + for (int end = 0; end < s.length(); end++) { + char currentChar = s[end]; + + // If the character is already in the map and is within the current window + if (charIndexMap.find(currentChar) != charIndexMap.end() && + charIndexMap[currentChar] >= start) { + start = charIndexMap[currentChar] + 1; } - return maxLength; + + // Update the last seen index of the current character + charIndexMap[currentChar] = end; + + // Calculate the max length of substring + maxLength = max(maxLength, end - start + 1); + } + return maxLength; } int main() { - string input; - cout << "Enter a string: "; - cin >> input; + string input; + cout << "Enter a string: "; + cin >> input; - int result = lengthOfLongestSubstring(input); - cout << "The length of the longest substring without repeating characters is: " << result << endl; + int result = lengthOfLongestSubstring(input); + cout + << "The length of the longest substring without repeating characters is: " + << result << endl; - return 0; + return 0; } /* Example: Input: "abcabcbb" Explanation: -- The longest substring without repeating characters is "abc", which has a length of 3. +- The longest substring without repeating characters is "abc", which has a +length of 3. - As we iterate through the string: - Start with "a" -> length 1 - Add "b" -> "ab" -> length 2 - Add "c" -> "abc" -> length 3 - - "a" repeats, so move the start to the next character -> "bca" (length still 3) + - "a" repeats, so move the start to the next character -> "bca" (length +still 3) - Continue until the end, with the longest substring remaining "abc". Output: 3 Input: "bbbbb" Explanation: -- The longest substring without repeating characters is "b", which has a length of 1. -Output: 1 +- The longest substring without repeating characters is "b", which has a length +of 1. Output: 1 */