From 99a222c31950a5b9a5c62a041da309d44c723ced Mon Sep 17 00:00:00 2001 From: Buduf Date: Thu, 14 Apr 2022 13:35:04 +0200 Subject: [PATCH] problem 3 --- LengthOfLongestSubstring.cs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 LengthOfLongestSubstring.cs diff --git a/LengthOfLongestSubstring.cs b/LengthOfLongestSubstring.cs new file mode 100644 index 0000000..7febdcd --- /dev/null +++ b/LengthOfLongestSubstring.cs @@ -0,0 +1,30 @@ +public class Solution { + public int LengthOfLongestSubstring(string s) { + int max = 0; + int n = 0; + for (int i = 0; i < s.Length; i++) + { + n++; + if (n > 1) + { + n -= FindDuplicateCharacter(s.Substring(i - n + 1, n)) + 1; + } + if (n > max) { + max = n; + } + } + return max; + } + + public int FindDuplicateCharacter(string s) { + char last = s[s.Length - 1]; + for (int i = 0; i < s.Length - 1; i++) + { + if (s[i] == last) + { + return i; + } + } + return -1; + } +} \ No newline at end of file