+ 1

Maximum length of the substring

Find the length of the substring which has no repeated characters present in the string.. Like If a="bbb" Then answer should be "b" And length should be 1. All the characters in the string are connected! I want answer in O(n) but got a complexity of O(n^2)

4th Apr 2018, 6:17 PM
Trivedi Rutansh
Trivedi Rutansh - avatar
12 Antworten
+ 1
a = "BartoszBar" result = "" j = 0 s = [] for i in range(0, len(a) - 1): while (j < i and a[i] in s): s.remove(a[j]) j = j + 1 s.insert(a[i]) if (i - j + 1 > len(result)): result = a[j:i+1] print result Now it is linear.
4th Apr 2018, 6:49 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 1
Nah, it is O(nlogn). It is not much slower than O(n)
4th Apr 2018, 6:52 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 1
Now it is linear. Used list instead of set
4th Apr 2018, 6:54 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Are in substring all letters connected or not necessary?
4th Apr 2018, 6:29 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Yes all the letters are connected
4th Apr 2018, 6:30 PM
Trivedi Rutansh
Trivedi Rutansh - avatar
0
You can bruteforce it. Longest answer is 26(alphabet size). Then start in any position and go on 26 letters ahead. O(26*N)=O(N)
4th Apr 2018, 6:32 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Brother can you please simplify your answer? If user enters string value = "BartoszBar" Then our method should return "Bartosz" and length of 7 with O(n) Complexity
4th Apr 2018, 6:34 PM
Trivedi Rutansh
Trivedi Rutansh - avatar
0
Wait, I will write code.
4th Apr 2018, 6:35 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Okay thanks 😅
4th Apr 2018, 6:39 PM
Trivedi Rutansh
Trivedi Rutansh - avatar
0
Bro complexity is O(n^2)
4th Apr 2018, 6:51 PM
Trivedi Rutansh
Trivedi Rutansh - avatar
0
I want O(n) and it is feasible..😬
4th Apr 2018, 6:52 PM
Trivedi Rutansh
Trivedi Rutansh - avatar
0
Got it..Thanks ✌🏻😬
4th Apr 2018, 6:56 PM
Trivedi Rutansh
Trivedi Rutansh - avatar