Advertisement
CR7CR7

StringFinder

May 31st, 2023
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class TitleFinder {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         // Read the title string
  8.         String title = scanner.nextLine();
  9.         // Read the number of lines
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.         // Loop through the lines
  12.         for (int i = 0; i < n; i++) {
  13.             // Read the current line
  14.             String line = scanner.nextLine();
  15.             // Check if the line is a subsequence of the title
  16.             if (isSubsequence(line, title)) {
  17.                 // Remove the line from the title
  18.                 title = removeSubsequence(line, title);
  19.                 // Print the modified title
  20.                 System.out.println(title);
  21.             } else {
  22.                 // Print a message
  23.                 System.out.println("No such title found!");
  24.             }
  25.         }
  26.     }
  27.  
  28.     // A helper method to check if a string is a subsequence of another string
  29.     public static boolean isSubsequence(String s1, String s2) {
  30.         // Initialize two pointers for s1 and s2
  31.         int i = 0;
  32.         int j = 0;
  33.         // Loop until one of the strings is exhausted
  34.         while (i < s1.length() && j < s2.length()) {
  35.             // If the characters match, move both pointers forward
  36.             if (s1.charAt(i) == s2.charAt(j)) {
  37.                 i++;
  38.                 j++;
  39.             } else {
  40.                 // Otherwise, move only the pointer for s2 forward
  41.                 j++;
  42.             }
  43.         }
  44.         // Return true if all characters of s1 are matched
  45.         return i == s1.length();
  46.     }
  47.  
  48.     // A helper method to remove a subsequence from a string
  49.     public static String removeSubsequence(String s1, String s2) {
  50.         // Initialize a string builder to store the result
  51.         StringBuilder sb = new StringBuilder();
  52.         // Initialize two pointers for s1 and s2
  53.         int i = 0;
  54.         int j = 0;
  55.         // Loop until one of the strings is exhausted
  56.         while (i < s1.length() && j < s2.length()) {
  57.             // If the characters match, skip them and move both pointers forward
  58.             if (s1.charAt(i) == s2.charAt(j)) {
  59.                 i++;
  60.                 j++;
  61.             } else {
  62.                 // Otherwise, append the character from s2 to the result and move only the pointer for s2 forward
  63.                 sb.append(s2.charAt(j));
  64.                 j++;
  65.             }
  66.         }
  67.         // Append the remaining characters from s2 to the result
  68.         sb.append(s2.substring(j));
  69.         // Return the result as a string
  70.         return sb.toString();
  71.     }
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement