Repository: TheAlgorithms/C-Plus-Plus
File: others/longest_substring_without_repeating_characters.cpp (C++)

Code before:
 * 1) Initialize an unordered_map to track the frequency of characters.
 * 2) Use a deque for pushing characters, and update the result deque (`res`) with the current deque (`temp`) 
 *    whenever we find a longer substring.
 * 3) Use a while loop to reduce the frequency from the front, incrementing `i`, 
 *    and removing characters from the `temp` deque as we no longer need them.
 * 4) Return `res.size()` as we are interested in the length of the longest substring.
 * 
 * Time Complexity: O(N)
 * Space Complexity: O(N)
 * 
 * Examples:
 * - testcase1: s = "abcabcbb", output = 3.
 * - testcase2: s = "bbbbb", output = 1.
 * - testcase3: s = "pwwkew", output = 3.
 * 
 * I hope this helps to understand.
 * Thank you!
 **/

// ----------------- Header files ----------------------------------
#include <iostream>        // for input and output read/write.
#include <unordered_map>   // to use it for character frequency.
#include <deque>           // for push and pop operations at O(1) time.
#include <string>          // for taking string as input.

using namespace std;       // using the namespace standard to reduce redundant usage of std::

//------------------ Longest_Substring Class -------------------------------
/**
 * @class Solution
 * @brief Class that solves the Longest Substring Without Repeating Characters problem.
 */
class Longest_Substring {
public:
    /**
     * @brief Function to find the length of the longest substring without repeating characters.
     * @param s Input string.
     * @returns Length of the longest substring.
     */
    int lengthOfLongestSubstring(string s) {
        // If the size of string is 1, then it will be the answer.
        if (s.size() == 1) return 1;

        // Map used to store the character frequency.
        unordered_map<char, int> m;
        int n = s.length();

        // Deque to remove from back if repeating characters are present.
        deque<char> temp;
        deque<char> res;
        int i, j;

Review comment (suggestion):
```suggestion
```
using namespace std is bad practice and hence not allowed on this repository.

Code after:
 *    whenever we find a longer substring.
 * 3) Use a while loop to reduce the frequency from the front, incrementing `i`, 
 *    and removing characters from the `temp` deque as we no longer need them.
 * 4) Return `res.size()` as we are interested in the length of the longest substring.
 * 
 * Time Complexity: O(N)
 * Space Complexity: O(N)
 * 
 * I hope this helps to understand.
 * Thank you!
 * @author [Ashish Kumar Sahoo](github.com/ashish5kmax)
 **/

#include <iostream>        // for IO Operations
#include <unordered_map>   // for std::unordered_map
#include <deque>           // for std::deque
#include <string>          // for string class/string datatype which is taken as input
#include <cassert>         // for assert

/**
 * @class Longest_Substring
 * @brief Class that solves the Longest Substring Without Repeating Characters problem.
 */
class Longest_Substring {
public:
    /**
     * @brief Function to find the length of the longest substring without repeating characters.
     * @param s Input string.
     * @return Length of the longest substring.
     */
    int lengthOfLongestSubstring(std::string s) {
        // If the size of string is 1, then it will be the answer.
        if (s.size() == 1) return 1;

        // Map used to store the character frequency.
        std::unordered_map<char, int> m;
        int n = s.length();

        // Deque to remove from back if repeating characters are present.
        std::deque<char> temp;
        std::deque<char> res;
        int i, j;

        // Sliding window approach using two pointers.
        for (i = 0, j = 0; i < n && j < n;) {
            m[s[j]]++;

            // If repeating character found, update result and remove from the front.
            if (m[s[j]] > 1) {
                if (temp.size() > res.size()) {
                    res = temp;