Skip to content

Commit af9e92c

Browse files
Add files via upload
1 parent 8186b51 commit af9e92c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Runtime: 8 ms, faster than 94.43% of C++ online submissions for Longest Substring Without Repeating Characters.
2+
// Memory Usage: 9.9 MB, less than 74.13% of C++ online submissions for Longest Substring Without Repeating Characters.
3+
4+
class Solution
5+
{
6+
public:
7+
int lengthOfLongestSubstring(string s)
8+
{
9+
int maxLength = 0;
10+
int rightPtr = s.length() - 1;
11+
12+
vector<int> memo(128, -1);
13+
for (int leftPtr = s.length() - 1; leftPtr >= 0; --leftPtr)
14+
{
15+
if (memo[s[leftPtr]] != -1 && memo[s[leftPtr]] <= rightPtr)
16+
rightPtr = memo[s[leftPtr]] - 1;
17+
memo[s[leftPtr]] = leftPtr;
18+
maxLength = max(maxLength, rightPtr - leftPtr + 1);
19+
}
20+
21+
return maxLength;
22+
}
23+
};

0 commit comments

Comments
 (0)