String Compare
hi all,
I Need to check two strings , whether they differ by 3 contiguos characters or not.
I have written my logic as this ,But it isnt working, Can anyone help me out in correcting this.
int pwdCount =0;
for(int pwdC = 0;pwdC < newpwdArr.length()-2 ;pwdC++)
{
boolean boolPwd =false;
for(int pwdP = 0;pwdP < passwdArr.length()-2;pwdP++)
{
if( ((newpwdArr.charAt(pwdC)) == (passwdArr.charAt(pwdP))) &&
((newpwdArr.charAt(pwdC+1)) == (passwdArr.charAt(pwdP+1))) &&
((newpwdArr.charAt(pwdC+2)) == (passwdArr.charAt(pwdP+2))) ){
boolPwd =true;
}
}
if(!boolPwd)
pwdCount ++;
}
if(pwdCount < 3)
System.out.println("Its contiguos");
else
System.out.println("No");
[1426 byte] By [
Manthanaa] at [2007-11-27 10:12:20]

i.e 3 continuos characters should differ when compared.
example
if
String A = abc+1234
String B = abc-1234
This doesn't satisfy our requirement
if A= abc+1234
B= abc+3412
is Acceptable since 123 is replaced by 341
And
A = abc+1234
B= abd-5234
is also acceptable
But
A=abc+1234
B=dbf+6234
is not acceptable eventhough three characters(a,c,1) are different, since they are not contiguous.
public static void main(String[] args) {
String a = "abc+1234";
String b = "abc-1234";
System.out.println(contiguousDifference(a, b, 3));
b = "abc+3412";
System.out.println(contiguousDifference(a, b, 3));
b = "abd-5234";
System.out.println(contiguousDifference(a, b, 3));
b = "dbf+6234";
System.out.println(contiguousDifference(a, b, 3));
}
/**
* @param n the number of characters in a row that need to be different
*/
public static boolean contiguousDifference(String a, String b, int n) {
int difference = Math.abs(a.length() - b.length());
if (difference >= n) {
return true;
}
int count = 0;
for (int i = 0; i < a.length() && i < b.length(); i++) {
if (count >= n) {
return true;
}
if (a.charAt(i) != b.charAt(i)) {
count++;
} else {
count = 0;
}
}
if (count > 0 && count + difference >= n) {
return false;
}
return false;
}
Message was edited by:
dwg
Fixed a slight error
dwga at 2007-7-28 15:19:37 >

> if (count > 0 && count + difference >= n) {
> return false;
> }
> Should that be
>
> return true;
> ?
>
> Beautiful solution! :-)
Yup that's right. Thanks.
dwga at 2007-7-28 15:19:37 >
