package day04.homework;
public class Test02 { public static void main(String[] args) { String[] strs ={"abc","cde","bcd","ac","bcd","ac"}; String str1 = "abc"; String str2 = "ac"; int index1 = -1; int index2 = -1; int min = strs.length; if (str1==null||str2==null){ min = -1; System.out.println(min); }else if (str1.equals(str2)){ min = 0; System.out.println(min); }else { for (int i = 0; i < strs.length ; i++) { if (strs[i].equals(str1)){ min=Math.min(min,index2==-1?min:i-index2); index1 =i; } if (strs[i].equals(str2)){ min=Math.min(min,index1==-1?min:i-index1); index2=i; } } if (min == strs.length){ System.out.println(-1); }else { System.out.println(min); } } } }
|