moh.ezz8
BAN USER
Comments (5)
Reputation 20
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
-1
of 1 vote
public static boolean hasDoubleSequence(String pwd)
{
for (int end1 = 0; end1 < pwd.length(); end1++)
{
int end2 = pwd.indexOf(pwd.charAt(end1),end1+1);
int len = end2 - end1;
System.out.println(end1+" "+end2);
if(end2 > -1 && len <= end1+1)
{
String s1 = pwd.substring(end1-len+1, end1+1);
String s2 = pwd.substring(end1+1, end2+1);
if(s1.equals(s2))
return true;
}
}
return false;
}
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
The key here is that the other n/2 elements are unique. (If they're not, it'd be a bit harder)
- moh.ezz8 March 06, 2013With space :
Use HashSet, if you find an element twice, then this is the element...(since all other elements are unique, none of them can repeat)
Without space :
A 3-element window should work. We slide the window over the array until we find a window with an element repeated twice or thrice >> that's our element.
It works because the n/2 other elements are UNIQUE.