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..cccd3c9 --- /dev/null +++ b/CPP/Sliding window/longest-substring-without-repeating-characters.cpp @@ -0,0 +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 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); + + mpp[s[right]] = right; + + len = max(len, right - left + 1); + right++; + } + return len; + } +}; + +int main() { + string str = "takeUforward"; + Solution obj; + cout << "The length of the longest substring without repeating characters is " << obj.lengthofLongestSubstring(str); + return 0; +} \ No newline at end of file