|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief Solution for Longest Substring Without Repeating Characters problem. |
| 4 | + * @details |
| 5 | + * Problem link: https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ |
| 6 | + * |
| 7 | + * Intuition: |
| 8 | + * 1) The intuition is straightforward and simple. We track the frequency of characters. |
| 9 | + * 2) Since we can't use a string to track the longest substring without repeating characters efficiently (as removing a character from the front of a string isn't O(1)), we optimize the solution using a deque approach. |
| 10 | + * |
| 11 | + * Approach: |
| 12 | + * 1) Initialize an unordered_map to track the frequency of characters. |
| 13 | + * 2) Use a deque for pushing characters, and update the result deque (`res`) with the current deque (`temp`) |
| 14 | + * whenever we find a longer substring. |
| 15 | + * 3) Use a while loop to reduce the frequency from the front, incrementing `i`, |
| 16 | + * and removing characters from the `temp` deque as we no longer need them. |
| 17 | + * 4) Return `res.size()` as we are interested in the length of the longest substring. |
| 18 | + * |
| 19 | + * Time Complexity: O(N) |
| 20 | + * Space Complexity: O(N) |
| 21 | + * |
| 22 | + * I hope this helps to understand. |
| 23 | + * Thank you! |
| 24 | + * @author [Ashish Kumar Sahoo](github.com/ashish5kmax) |
| 25 | + **/ |
| 26 | + |
| 27 | +#include <iostream> // for IO Operations |
| 28 | +#include <unordered_map> // for std::unordered_map |
| 29 | +#include <deque> // for std::deque |
| 30 | +#include <string> // for string class/string datatype which is taken as input |
| 31 | +#include <cassert> // for assert |
| 32 | + |
| 33 | +/** |
| 34 | + * @class Longest_Substring |
| 35 | + * @brief Class that solves the Longest Substring Without Repeating Characters problem. |
| 36 | + */ |
| 37 | +class Longest_Substring { |
| 38 | +public: |
| 39 | + /** |
| 40 | + * @brief Function to find the length of the longest substring without repeating characters. |
| 41 | + * @param s Input string. |
| 42 | + * @return Length of the longest substring. |
| 43 | + */ |
| 44 | + int lengthOfLongestSubstring(std::string s) { |
| 45 | + // If the size of string is 1, then it will be the answer. |
| 46 | + if (s.size() == 1) return 1; |
| 47 | + |
| 48 | + // Map used to store the character frequency. |
| 49 | + std::unordered_map<char, int> m; |
| 50 | + int n = s.length(); |
| 51 | + |
| 52 | + // Deque to remove from back if repeating characters are present. |
| 53 | + std::deque<char> temp; |
| 54 | + std::deque<char> res; |
| 55 | + int i, j; |
| 56 | + |
| 57 | + // Sliding window approach using two pointers. |
| 58 | + for (i = 0, j = 0; i < n && j < n;) { |
| 59 | + m[s[j]]++; |
| 60 | + |
| 61 | + // If repeating character found, update result and remove from the front. |
| 62 | + if (m[s[j]] > 1) { |
| 63 | + if (temp.size() > res.size()) { |
| 64 | + res = temp; |
| 65 | + } |
| 66 | + |
| 67 | + while (m[s[j]] > 1) { |
| 68 | + temp.pop_front(); |
| 69 | + m[s[i]]--; |
| 70 | + i++; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Add the current character to the deque. |
| 75 | + temp.push_back(s[j]); |
| 76 | + j++; |
| 77 | + } |
| 78 | + |
| 79 | + // Final check to update result. |
| 80 | + if (temp.size() > res.size()) { |
| 81 | + res = temp; |
| 82 | + } |
| 83 | + |
| 84 | + return res.size(); // Return the length of the longest substring. |
| 85 | + } |
| 86 | +}; |
| 87 | + |
| 88 | +/** |
| 89 | + * @brief Self-test implementations |
| 90 | + * @returns void |
| 91 | + */ |
| 92 | +static void tests() { |
| 93 | + Longest_Substring soln; |
| 94 | + assert(soln.lengthOfLongestSubstring("abcabcbb") == 3); |
| 95 | + assert(soln.lengthOfLongestSubstring("bbbbb") == 1); |
| 96 | + assert(soln.lengthOfLongestSubstring("pwwkew") == 3); |
| 97 | + assert(soln.lengthOfLongestSubstring("") == 0); // Test case for empty string |
| 98 | + assert(soln.lengthOfLongestSubstring("abcdef") == 6); // Test case for all unique characters |
| 99 | + assert(soln.lengthOfLongestSubstring("a") == 1); // Single character |
| 100 | + std::cout << "All test cases passed!" << std::endl; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * @brief Main function. |
| 105 | + * @return 0 on successful execution. |
| 106 | + */ |
| 107 | +int main() { |
| 108 | + tests(); // run self-test implementations |
| 109 | + return 0; |
| 110 | +} |
0 commit comments