Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MinDist {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String s = in.nextLine();
- char x = in.next().charAt(0), y = in.next().charAt(0);
- int k=0,min,pozx,pozy;
- int min1=1000000;
- int ch=s.length();
- while (k<ch){
- if (x==s.charAt(k)){
- pozx=k;
- k++;
- while (k<ch && s.charAt(k)!=y){
- if (s.charAt(k)==x)
- pozx=k;
- k++;
- }
- if (k==ch) {
- break;
- }
- pozy=k;
- min=pozy-pozx-1;
- if (min<min1)
- min1=min;
- }
- if (y==s.charAt(k)){
- pozy=k;
- k++;
- while (k<ch && s.charAt(k)!=x){
- if (s.charAt(k)==y)
- pozy=k;
- k++;
- }
- if (k==ch) {
- break;
- }
- pozx=k;
- min=pozx-pozy-1;
- if (min<min1)
- min1=min;
- }
- k++;
- }
- if (k==ch)
- System.out.println(min1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement