I am attempting the Longest Substring Without Repeating Characters(https://leetcode.com/problems/longest-substring-without-repeating-characters/description/) problem. So it has been failing the 3rd test case where length of non repeating substring from ‘pwwkew’ should be 3. I created my code, which uses two pointers,
I start from first index comparing it with last index, until I get e match I increment i and move forward to 2nd index, 3rd index and so on, when I do get a match I split the string there, and continue it recursively on the 2 strings, kind of like quick sort. if my string is of length 2 and s[i]==s[j] i return 1 for test case 2.
When I ran it in VS code I got proper answers, 3,1,3 for all the cases, but the Leet Code is saying my output for 3rd case is 1. This is very confusing. How should I approach this problem.
“
Inputstr= "pwwkew"
class Solution:
c = 50
def lengthOfLongestSubstring(self, s: str) -> int:
i = 0
j = len(s)-1
while i<j:
if s[i]==s[j]:
if len(s)==2:
print(s)
Solution.c = 1
self.lengthOfLongestSubstring(s[:i+1])
self.lengthOfLongestSubstring(s[i+1:])
Solution.c = min(Solution.c,len(s))
i=i+1
return Solution.c
a= Solution()
print(a.lengthOfLongestSubstring(Inputstr))
“
I am expecting this to return 3 in last test case, I am planning to use recursion and divide and conquer technique here instead of sliding window