From 8411d263e1acd14464d9a045812076c799242f91 Mon Sep 17 00:00:00 2001 From: Anand Soni Date: Thu, 12 Oct 2023 15:32:01 +0530 Subject: [PATCH] String medium level --- ...t_substr_without_repeating_characters.cpp" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "leetcode/easy \360\237\231\205\342\200\215\342\231\202\357\270\217/questions \360\237\216\257/string/longest_substr_without_repeating_characters.cpp" diff --git "a/leetcode/easy \360\237\231\205\342\200\215\342\231\202\357\270\217/questions \360\237\216\257/string/longest_substr_without_repeating_characters.cpp" "b/leetcode/easy \360\237\231\205\342\200\215\342\231\202\357\270\217/questions \360\237\216\257/string/longest_substr_without_repeating_characters.cpp" new file mode 100644 index 0000000..1e4ba49 --- /dev/null +++ "b/leetcode/easy \360\237\231\205\342\200\215\342\231\202\357\270\217/questions \360\237\216\257/string/longest_substr_without_repeating_characters.cpp" @@ -0,0 +1,32 @@ +// Problem Link - https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ + +#include +#include +#include +using namespace std; +int lengthOfLongestSubstr(string str) +{ + set st; + int i = 0, j = 0, maxi = 0; + while(i < str.length() && j < str.length()) + { + if(st.empty() || st.find(str[j]) == st.end()) + { + st.insert(str[j]); + maxi = max(maxi, j - i + 1); + j++; + } + else + { + st.erase(str[i]); + i++; + } + } + return maxi; +} +int main() +{ + string str; cin >> str; + cout << lengthOfLongestSubstr(str) << endl; + return 0; +} \ No newline at end of file