vladimirVenkov

Subsequence

Jun 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Subsequence {
  4.     public static void main(String[] args) {
  5.         Scanner input = new Scanner(System.in);
  6.         String key = input.nextLine();
  7.         String code = input.nextLine();
  8.         if(key.length()>code.length()){
  9.             System.out.println(false);
  10.         }
  11.         else if(key.length()!=0){
  12.             findKey(0, key, code, 0);
  13.         }
  14.  
  15.  
  16.  
  17.     }
  18.  
  19.     private static void findKey(int index, String key, String code, int codeIndex) {
  20.         StringBuilder sb = new StringBuilder();
  21.         if (key.length() == index && codeIndex < code.length()) {
  22.             System.out.println(true);
  23.             return;
  24.         }
  25.         for (int i = codeIndex; i < code.length(); i++) {
  26.             if (key.charAt(index) == code.charAt(i)) {
  27.                 codeIndex = i;
  28.                 sb.append(key.charAt(index));
  29.                 findKey(index + 1, key, code, codeIndex);
  30.                 return;
  31.             }
  32.  
  33.         }
  34.         System.out.println(false);
  35.         return;
  36.     }
  37. }
Add Comment
Please, Sign In to add comment