Skip to main content
Back to problems
Codeforces
Easy
Strings
Hash Maps
Lucky Substring

Find the shortest substring made only of digits whose digit sum is 4 or 7.

Acceptance 0%
Problem Statement

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 s of 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| <= 100
  • s contains only characters 0-9.

(These are practice-oriented constraints inferred from the problem type.)

Examples
Sample cases returned by the problem API.

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.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.