I have two method, the difference is one is having --i1 and other is having i-1, however they are producing different output.
String s1 = "axbdcg";
String s2 ="aazzzzzzzzzzzzcbc";
System.out.println(method(s1,s2,s1.length()-1,s2.length()-1));
int method(String s1, String s2, int i1, int i2){
if(s1.length() == 0 || s2.length() == 0) return 0;
if(i1 < 0|| i2 < 0) return 0;
if(s1.charAt(i1) == s2.charAt(i2)) return 1+ method(s1,s2,--i1,--i2);
int left = method(s1,s2,--i1,i2);
int right = method(s1,s2,i1,--i2);
return Math.max(left ,right);
}
this method is giving 2 as output and
int method(String s1, String s2, int i1, int i2){
if(s1.length() == 0 || s2.length() == 0) return 0;
if(i1 < 0|| i2 < 0) return 0;
if(s1.charAt(i1) == s2.charAt(i2)) return 1+ method(s1,s2,--i1,--i2);
int left = method(s1,s2,i1-1,i2);
int right = method(s1,s2,i1,--i2);
return Math.max(left ,right);
}
is giving 3 as output.
foo(--i)
means following:
i
with -1
i
i
to the method foo()
P.S. i
is modified
foo(i - 1)
means following:
i
with -1
i
to the method foo()
P.S. i
is not modified
So the difference is when you call method(s1,s2,i1,--i2)
in the first snippet, i
is being changed, but in the second one - is not.