Chef loves games! But he likes to invent his own. Now he plays game "Digit Jump". Chef…
- User
Chef loves games! But he likes to invent his own. Now he plays game "Digit Jump". Chef has sequence of digits S1, S2,..., SN,. He is staying in the first digit (S1) and want to reach the last digit (SN) in the minimal number of jumps. While staying in some digit x with index i (digit Si) Chef can jump into digits with indices i - 1 (Si-1) and i + 1 (Si+1) but he can't jump out from sequence. Or he can jump into any digit with the same value x. Help Chef to find the minimal number of jumps he need to reach digit SN from digit S1. Input Input contains a single line consist of string S of length N- the sequence of digits.
Output In a single line print single integer - the minimal number of jumps he needs.
Constraints
1 ≤ N ≤ 10^5 Each symbol of S is a digit from 0 to 9.
Example Input: 01234567890
Output: 1
Input: 012134444444443
Output: 4
Explanation In the first case Chef can directly jump from the first digit (it is 0) to the last (as it is also 0). In the second case Chef should jump in such sequence (the number of digits from 1: 1-2-4-5-15).
Assistant
pythons = input().strip() n = len(s) if n == 1: print(0) else: from collections import deque digit_indices = {} for i, c in enumerate(s): if c not in digit_indices: digit_indices[c] = [] digit_indices[c].append(i) visited = [False] * n q = deque() q.append((0, 0)) visited[0] = True while q: current, steps = q.popleft() if current == n - 1: print(steps) break # Check adjacent indices for neighbor in [current - 1, current + 1]: if 0 <= neighbor < n and not visited[neighbor]: visited[neighbor] = True q.append((neighbor, steps + 1)) # Check same digits current_char = s[current] if current_char in digit_indices: for idx in digit_indices[current_char]: if not visited[idx]: visited[idx] = True q.append((idx, steps + 1)) # Remove to prevent reprocessing del digit_indices[current_char]
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.