Untitled
unknown
plain_text
6 days ago
2.8 kB
2
Indexable
Never
import java.util.*; public class Main { public static boolean StringChallenge(String str) { // Split the input string into two parts: the pattern and the matching string String[] parts = str.split(" "); if (parts.length != 2) return false; // Ensure we have two parts String pattern = parts[0]; String matchString = parts[1]; int i = 0, j = 0; while (i < pattern.length() && j < matchString.length()) { char p = pattern.charAt(i); // Handle the '+' symbol (any single alphabetic character) if (p == '+') { if (!Character.isAlphabetic(matchString.charAt(j))) return false; j++; // Move to the next character in matchString } // Handle the '$' symbol (a single digit from 1 to 9) else if (p == '$') { if (!Character.isDigit(matchString.charAt(j)) || matchString.charAt(j) == '0') return false; j++; // Move to the next character in matchString } // Handle the '*' symbol (sequence of exactly 3 of the same character) else if (p == '*') { char sequenceChar = matchString.charAt(j); // The character to repeat int repeatCount = 3; // Default repeat count is 3 // Check if the pattern is followed by {N} if (i + 1 < pattern.length() && pattern.charAt(i + 1) == '{') { int closingBrace = pattern.indexOf('}', i); if (closingBrace != -1) { // Extract the number N inside {N} String numStr = pattern.substring(i + 2, closingBrace); repeatCount = Integer.parseInt(numStr); i = closingBrace; // Move the pointer to after the closing brace } } // Check if the sequence in matchString is exactly `repeatCount` of the same character for (int k = 0; k < repeatCount; k++) { if (j >= matchString.length() || matchString.charAt(j) != sequenceChar) { return false; } j++; // Move to the next character in matchString } } // Increment the pattern pointer i++; } // Ensure both the pattern and the matching string are fully processed return i == pattern.length() && j == matchString.length(); } public static void main(String[] args) { // Test cases Scanner s = new Scanner(System.in); System.out.println(StringChallenge(s.nextLine())); // Read the input from the console } }
Leave a Comment