-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
30 lines (28 loc) · 851 Bytes
/
Solution.cs
File metadata and controls
30 lines (28 loc) · 851 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
namespace LeetCode.Medium.Problem3{
//3. Longest Substring Without Repeating Characters
//https://leetcode.com/problems/longest-substring-without-repeating-characters/
/*
Given a string s, find the length of the longest
substring
without repeating characters.
*/
public class Solution {
public int LengthOfLongestSubstring(string s) {
int ans = 0;
int left = 0;
Dictionary<char,int> dic = new Dictionary<char,int>();
for (int right = 0; right < s.Length; right++)
{
if (dic.ContainsKey(s[right]))
{
left = Math.Max(dic[s[right]] + 1, left);
dic[s[right]] = right;
}
else
dic.Add(s[right],right);
ans = Math.Max(ans, right - left +1);
}
return ans;
}
}
}