Official

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

swe-2-high

Lucky Number

Solution

Simply count how many times the character 7 appears in the string \(S\), and print Yes if that count is at least \(K\), otherwise No.

Concretely:

  1. Read \(N\), \(K\), and the string \(S\) from the input.
  2. Initialize a counter \(cnt\) to \(0\).
  3. Scan each character of \(S\) in order; every time it is '7', increment \(cnt\) by \(1\).
  4. If \(cnt \geq K\), print Yes; otherwise print No.

In Python, str.count('7') gives the same result directly.

Proof of Correctness

For each character of \(S\), \(cnt\) is incremented exactly once if and only if the character is '7'. Hence, when the loop finishes, \(cnt\) equals exactly the number of occurrences of '7' in \(S\). The problem’s winning condition is “the number contains at least \(K\) occurrences of 7”, so the test cnt >= K is equivalent to the ticket being a winner. Therefore the output is correct.

The constraints guarantee that \(S\) has length exactly \(N\), so iterating over \(S\) directly is equivalent to iterating \(N\) times; the implementation scans \(S\) directly.

Complexity

  • Time: \(O(N)\) — each character of \(S\) is examined once.
  • Space: \(O(N)\) for storing the input string \(S\); \(O(1)\) auxiliary space for the counter.

Since \(N \leq 100\), this is easily fast enough.

Implementation Notes

  • The output strings are Yes and No with the first letter capitalized.
  • Although \(N\) equals the length of \(S\) by the constraints, using the actual length of \(S\) is simpler and safe.
  • There is no tie-breaking: a count equal to exactly \(K\) still prints Yes.

posted:
last update: