Find the shortest substring made only of digits whose digit sum is 4 or 7.
Problem
You are given a string consisting only of decimal digits. A substring is called lucky if the sum of its digits is either 4 or 7.
Find the length of the shortest lucky substring. If no lucky substring exists, report that it is impossible.
Because all characters are digits, the task is to examine contiguous parts of the string and determine whether their digit sum matches one of the lucky values.
Input Format
- A single line containing a string
sof digits.
Interpretation
- A substring is a contiguous segment of
s. - The sum of a substring is the sum of its digit values.
Output Format
- Print the minimum length of a lucky substring.
- If no lucky substring exists, print
-1.
Constraints
1 <= |s| <= 100scontains only characters0-9.
(These are practice-oriented constraints inferred from the problem type.)
Example 1
Input
4
Output
1
Explanation
The single character substring "4" has digit sum 4, so the answer is 1.
Example 2
Input
123
Output
-1
Explanation
No contiguous substring has digit sum 4 or 7.
Show 1 more example
Example 3
Input
16
Output
2
Explanation
The substring "16" has digit sum 7, so the shortest lucky substring has length 2.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.