Official

A - 幸運の番号 / Lucky Number Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

Count the number of occurrences of 7 in the string \(S\), and output Yes if it is at least \(K\), and No otherwise.

Analysis

Whether it is a winning ticket does not depend on the positions of 7 or whether they are consecutive; it is determined solely by the total count of 7s.

For example, when \(S =\) 70717 and \(K = 3\), there are \(3\) occurrences of 7, so the answer is Yes.

A straightforward approach that scans the string from the beginning to count the occurrences of 7 is sufficient. Since \(N \leq 100\), there is no need to worry about exceeding the time limit. In Python, this operation can be written concisely using S.count('7').

However, note that the winning condition is “at least \(K\) occurrences,” not “exactly \(K\) occurrences.” If you only check whether the count is equal to \(K\), it will result in a Wrong Answer when there are more than \(K\) occurrences.

Algorithm

  1. Read \(N\), \(K\), and \(S\).
  2. Count the occurrences of 7 using S.count('7') and assign it to cnt.
  3. If cnt >= K, output Yes; otherwise, output No.

Since cnt is the total number of 7s contained in \(S\), checking cnt >= K matches the problem’s winning condition.

Complexity

  • Time Complexity: \(O(N)\)
    S.count('7') scans the string of length \(N\).
  • Space Complexity: \(O(N)\)
    Required to store the input string \(S\). The additional space needed for counting and checking is \(O(1)\).

Implementation Details

  • Treat \(S\) as a string without converting it to an integer. This allows leading 0s to be read properly.

  • Pass the string '7' rather than the integer 7 to count.

  • Use .strip() to remove the trailing newline character from the input line.

  • The output is case-sensitive, so make sure to output Yes or No exactly as specified.

    Source Code

def main():
    import sys
    input = sys.stdin.readline
    N, K = map(int, input().split())
    S = input().strip()
    cnt = S.count('7')
    print("Yes" if cnt >= K else "No")

main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: