From e311a2cb6e3355f126ad3e3008fa8ee240f5928d Mon Sep 17 00:00:00 2001 From: Aishwarya Karanth <35144851+AishwaryaKaranth@users.noreply.github.com> Date: Sat, 24 Oct 2020 11:42:24 +0530 Subject: [PATCH] Create LongestSubstringWithoutRepeatingCharacters.py Added a GeeksForGeeks Solution --- ...gestSubstringWithoutRepeatingCharacters.py | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 GeeksforGeeks/Algorithm/DP/LongestSubstringWithoutRepeatingCharacters.py diff --git a/GeeksforGeeks/Algorithm/DP/LongestSubstringWithoutRepeatingCharacters.py b/GeeksforGeeks/Algorithm/DP/LongestSubstringWithoutRepeatingCharacters.py new file mode 100644 index 0000000..dd72213 --- /dev/null +++ b/GeeksforGeeks/Algorithm/DP/LongestSubstringWithoutRepeatingCharacters.py @@ -0,0 +1,33 @@ +''' +Given a string S, find the length of its longest substring that does not have any repeating characters. + +Example 1: + +Input: +S = geeksforgeeks +Output: 7 +Explanation: The longest substring +without repeated characters is "ksforge". +Example 2: + +Input: +S = abbcdb +Output: 3 +Explanation: The longest substring is +"bcd". Here "abcd" is not a substring +of the given string. +''' + + +def SubsequenceLength(s): + v={} + l=0 + maxlen=0 + for i in range(len(s)): + if s[i] in v and l<=v[s[i]]: + l=v[s[i]]+1 + else: + maxlen=max(maxlen,i-l+1) + v[s[i]]=i + return maxlen +