You are here : InfyTQ Archive >> InfyTQ Coding Problems
Find longest substring of unique characters which is case insensitive.
For “ABDEFGABEF”, the longest substring are “BDEFGA” and “DEFGAB”, with length 6.
For “BBBB”, the longest substring is “B”, with length 1.
For “CDEF”, the longest substring is “CDEF” with length 4.
Input :
First Input : String
Output : Length of longest substring
Sample Testcases :
I/P 1:
CDEF
O/P 1:
4
Solution provided by our user :
l=input()
len1=len(l)
a=[]
maxi=[]
for i in range(len1):
for j in range(i,len1):
if l[j] in a:
break;
else:
a.append(l[j])
if len(maxi)<=len(a):
maxi=a
a=[]
else:
a=[]
print(len(maxi))
#this is my code it worked
l=input(“enter the string: “)
len1=len(l)
a=[]
maxi=[]
for i in range(len1):
for j in range(i,len1):
if l[j] in a:
break;
else:
a.append(l[j])
if len(maxi)<=len(a):
maxi=a
a=[]
else:
a=[]
print(maxi)
sorry about the tab spaces.
step1. Change the string in upper case.(for avoiding the case sensitive result,avoid it if input is always in upper cases or lower cases)
step2. Convert the string into Set and print its length.
here is two line code.
string=input()
print(len(set(string.upper())))
This is wrong …try ABBCD..
i think your solutiom won’t work for all testcases
consider the one:ABCDEFFDCMNOPQ
rectify me if am wrong
import java.util.*;
import java.io.*;
public class LongestSubstring {
public static void main(String[] args)throws Exception {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
System.out.println(Norepeat(s));
}
public static String Norepeat(String input) {
Map visited = new HashMap();
String output = “”;
for (int start = 0, end = 0; end < input.length(); end++) {
char currChar = input.charAt(end);
if (visited.containsKey(currChar)) {
start = Math.max(visited.get(currChar)+1, start);
}
if (output.length() < end – start + 1) {
output = input.substring(start, end + 1);
}
visited.put(currChar, end);
}
return output;
}
}
s=input()
l=[]
if len(s)==len(set(s)):
print(len(s))
elif len(set(s))==1:
print(1)
else:
for i in range(len(s)):
for j in range(i+1,len(s)):
if s[i]==s[j]:
l.append(len(s[i+1:j+1]))
print(max(l))
s=input()
t=””
m=0
for i in range(len(s)):
for j in range(i,len(s)):
if s[j] in t:
if m<len(t):
m=len(t)
t=""
t+=s[j]
print(m)
/*package whatever //do not write package name here */
import java.io.*;
import java.util.*;
class GFG {
public static void main (String[] args) {
String input=”BBBBB”;
ArrayList al= new ArrayList();
int maxL=0;
for (int i=0;i<input.length();i++ ){
for(int j=i;jmaxL){
maxL=al.size();
al.clear();
}
else
al.clear();
}
System.out.println(maxL);
}
}