Find the shortest substring made only of digits whose digit sum is 4 or 7.
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.
s of digits.s.-1.1 <= |s| <= 100s contains only characters 0-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.
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
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.