EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2024
  3. Preliminary Round

Codey and Manuscript

https://www.hackerrank.com/contests/codenection-2024-preliminary-round-open-category/challenges/cn24-7/problem

PreviousCodey and RectanglesNextCodey and Recipes

Last updated 2 months ago

Question

Codey, a scholar from the kingdom of CodeNection, has been given an important mission by Queen Zoey. Queen Zoey recently uncovered an ancient manuscript that holds a string s, believed to contain a hidden message about the future king of the kingdom. However, parts of the manuscript are missing, with these gaps marked by * where letters once were.

To help restore the message, Queen Zoey gives Codey another string v, which is known to be part of the hidden message. Codey’s task is to determine whether it is possible to fill the gaps in s with lowercase English letters such that v appears as a subsequence of the s.

Codey must report back to Queen Zoey about his progress.

A string b is subsequence of string a if it's possible to remove some character in a to get b(without changing the order).

Input Format

The first line contains a single integer t, where t represents the number of test cases.

The first line of each test case contains a single string s, where s represents the damaged manuscript. It contains lowercase English letters and *.

The second line of each test case contains a single string v, where v represents the hidden message that must appear as a subsequence in the completed manuscript.

Constraints

1≤t≤1001 \le t \le 1001≤t≤100
1≤∣s∣≤1051 \le |s| \le 10^51≤∣s∣≤105

Output Format

For each test case, if it’s possible to fulfill the conditions, output YES. Otherwise, output NO.

Sample Inputs:

Input

3
co**y
codey
ma**a
leo
z**y
o

Output

YES
NO
YES

Explanation

In the first test case, the missing characters * can be replaced with d and e to form codey.

In the second test case, it's impossible to fill in the gaps to form leo.

In the third test case, one of the missing character * can be replaced with o.


Solution - 2 pointers

This one is actually simple if you know 2 pointers technique. If someone doesn't know, here how it goes:

  1. Prepare 2 arrays, which is for u and v.

  2. Prepare 2 pointers on each array, and set it on first char.

  3. Check first array pointers,

    1. if the current char on first array == char on second array, second array moves 1 step.

    2. if the current chat on first array == '*', second array moves 1 step.

  4. After checking, first array moves 1 step, rinse and repeat.

  5. The checking ends when it hits one of two conditions:

    1. If second array finished traversing first, print "YES".

    2. If first array finished traversing first, print "NO".

    3. If both finished at the same time, second array takes precedence.

That's all for theory, time to coding!

def compare_words(word1, word2):
    p1, p2 = 0, 0
    n1, n2 = len(word1), len(word2)
    
    while p1 < n1 and p2 < n2:
        if word1[p1] == word2[p2] or word1[p1] == '*':
            p2 += 1
        p1 += 1
    return "YES" if p2 == n2 else "NO"

t = int(input().strip())
for a0 in range(t):
    m = input().strip()
    n = input().strip()
    print(compare_words(m, n))
1≤∣v∣≤∣s∣1 \le |v| \le |s|1≤∣v∣≤∣s∣

It is guaranteed that the sum of ∣s∣|s|∣s∣ across all test cases doesn't exceed 2∗1052 * 10^52∗105.

🇲🇾