Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

 @Test
public void testSubstringLength()
{

String s=”abcbb”;
String temp=””;
char []arr=s.toCharArray();
HashMap<Character,Integer> hm=new HashMap<Character,Integer>();
for (Character t:arr)
{

if(!hm.containsKey(t))
{
hm.put(t, 1);
}
}//end of for

Iterator itr=hm.entrySet().iterator();
String output =””;
while(itr.hasNext())
{
Map.Entry me=(Map.Entry)itr.next();
output=output+me.getKey();
}
System.out.println(“Longest substring seen is “+output+”Length of this substring is “+output.length());
}

Advertisements

About qainterviews

Software Geek,QA Expert,Blogger
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s