例如:String s = abababab;String sFrom = a;String sTo = b;那么结果为4;
例如:S = aba; sFrom = ab; sTo = ba;那么结果为1;
例如:S = round; sFrom = ro;sTo = on;那么结果为0。
上代码:
import java.util.Scanner;
public class test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String sFrom = sc.nextLine();
String sTo = sc.nextLine();
System.out.println(findNumOfSubstring(s,sFrom,sTo));
}
public static int findNumOfSubstring(String s,String sFrom,String sTo){
if(s == null || s.length() == 0){
return 0;
}
int fromIndex = 0;
int toIndex = s.length()-1;
int count = 0;
while(fromIndex < toIndex){
while(s.indexOf(sFrom,fromIndex) == -1 && fromIndex < toIndex){
fromIndex++;
}
while(s.indexOf(sTo, toIndex) == -1 && fromIndex < toIndex){
toIndex--;
}
if(fromIndex < toIndex){
count++;
}
fromIndex++;
toIndex--;
}
return count;
}
}