A - 幸運の番号 / Lucky Number 解説 by admin 印刷ページ
gpt-6-astra-mediumOverview
Count the number of 7s contained in the string \(S\), and output Yes if the count is at least \(K\), and No otherwise.
Analysis
Whether it is a win depends only on how many 7s it contains. The positions of the 7s and the types of other digits do not matter.
Therefore, a naive method of scanning the string from the beginning and counting the 7s is sufficient. Since \(N \leq 100\), this approach will not exceed the time limit.
In Python, you can count the number of 7s contained in the string s using s.count('7').
For example, when \(S =\) 70717, there are \(3\) 7s.
- If \(K = 3\), since \(3 \geq 3\), the answer is Yes.
- If \(K = 4\), since \(3 < 4\), the answer is No.
Since the condition is “at least \(K\)”, having exactly \(K\) is also a win. Using > k or == k will result in an incorrect answer, so use >= k.
Algorithm
- Read \(N, K\) and the string \(S\).
- Find the number of
7s contained in \(S\) usings.count('7'). - If the count obtained is at least \(K\), output
Yes; otherwise, outputNo.
Comparing the count with \(K\) directly corresponds to the winning condition in the problem statement, so this procedure makes the correct determination.
Complexity
- Time Complexity: \(O(N)\) —
countinspects the entire string. - Space Complexity: \(O(N)\) — required to store the input string \(S\). The additional space excluding storing the input is \(O(1)\).
Implementation Notes
Read \(S\) as a string rather than an integer. This naturally handles cases where there are leading
0s.Pass the string
'7'tocount, not the integer7.Although \(N\) is provided as the length of the string, you do not need to use it directly in the code because
countinspects the entire string.Ensure the output strictly matches the specified case:
YesorNo.Source Code
# 問題文から独立に実装
n, k = map(int, input().split())
s = input().strip()
print("Yes" if s.count('7') >= k else "No")
This editorial was generated by gpt-6-astra-medium.
投稿日時:
最終更新: