diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json index 8251c76..eac73b7 100644 --- a/.obsidian/core-plugins.json +++ b/.obsidian/core-plugins.json @@ -15,6 +15,5 @@ "markdown-importer", "outline", "word-count", - "open-with-default-app", "file-recovery" ] \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Longest-Substring-Without-Repeating-Characters.md b/OJ notes/pages/Leetcode Longest-Substring-Without-Repeating-Characters.md new file mode 100644 index 0000000..a912520 --- /dev/null +++ b/OJ notes/pages/Leetcode Longest-Substring-Without-Repeating-Characters.md @@ -0,0 +1,89 @@ +# Leetcode Longest-Substring-Without-Repeating-Characters + +#### 2022-07-14 09:33 + +> ##### Algorithms: +> #algorithm +> ##### Data structures: +> #DS #string +> ##### Difficulty: +> #coding_problem #difficulty-medium +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:# +``` + + + +##### Links: +- [Link to problem](https://leetcode.com/problems/longest-substring-without-repeating-characters/) +___ +### Problem + +Given a string `s`, find the length of the **longest substring** without repeating characters. + +#### Examples + +**Example 1:** + +**Input:** s = "abcabcbb" +**Output:** 3 +**Explanation:** The answer is "abc", with the length of 3. + +**Example 2:** + +**Input:** s = "bbbbb" +**Output:** 1 +**Explanation:** The answer is "b", with the length of 1. + +**Example 3:** + +**Input:** s = "pwwkew" +**Output:** 3 +**Explanation:** The answer is "wke", with the length of 3. +Notice that the answer must be a substring, "pwke" is a subsequence and not a substring. + +#### Constraints + +- `0 <= s.length <= 5 * 104` +- `s` consists of English letters, digits, symbols and spaces. + +### Thoughts + +> [!summary] +> This is a # + +Initially, I thought of a lineal algorithm + +### Solution + +Initial solution + +```cpp +class Solution { +public: + int lengthOfLongestSubstring(string s) { + unordered_map hMap; + int cur = 0; + int localMax = 0; + + for (int i = 0; i < s.size(); i++) { + if (hMap.find(s[i]) == hMap.end()) { + cur++; + localMax = max(cur, localMax); + hMap[s[i]] = i; + } else { + i = hMap[s[i]]; + hMap.clear(); + cur = 0; + } + } + return localMax; + } +}; +``` \ No newline at end of file