Seven-Segment Display 180° Rotation Symmetry
Arrays & HashingAuto-graded · 28 test casesLast updated
Determine if a sequence of seven-segment digits appears identical when the entire display is flipped upside down.
You have a digital display showing a sequence of decimal digits (0-9), each rendered on a standard seven-segment LED cell. Your task is to determine whether the entire display would look identical if physically rotated 180 degrees.
Each seven-segment cell contains these segments:
a: top horizontalb: top-right verticalc: bottom-right verticald: bottom horizontale: bottom-left verticalf: top-left verticalg: middle horizontal
The standard segment patterns for digits are:
| Digit | Active Segments |
|---|---|
| 0 | a, b, c, d, e, f |
| 1 | b, c |
| 2 | a, b, d, e, g |
| 3 | a, b, c, d, g |
| 4 | b, c, f, g |
| 5 | a, c, d, f, g |
| 6 | a, c, d, e, f, g |
| 7 | a, b, c |
| 8 | a, b, c, d, e, f, g |
| 9 | a, b, c, d, f, g |
When you rotate the entire display 180 degrees:
- The order of cells reverses (first becomes last, last becomes first)
- Within each cell, segments transform as follows:
a↔d(top and bottom swap)b↔e(top-right and bottom-left swap)c↔f(bottom-right and top-left swap)g→g(middle stays in place)
Return True if and only if the rotated display shows exactly the same digit sequence. Note that a rotated cell must produce a valid digit pattern (one of 0-9); if rotation produces an unrecognized pattern, the display cannot match.
Constraints
1 <= len(digits) <= 10^50 <= digits[i] <= 9
Examples
Example 1
Input: is_display_symmetric([6, 0, 9])
Output: True
Example 2
Input: is_display_symmetric([8, 1, 8])
Output: False
Example 3
Input: is_display_symmetric([2, 5])
Output: False
Hints
Hint 1
First determine which digits remain valid after their segments are rotated. Some digit patterns become invalid or transform into different digits.
Hint 2
The display reverses left-to-right when rotated, so the digit at position i must match what the digit at position n-1-i becomes after its segments are transformed.
Hint 3
Build a lookup table mapping each digit to what it becomes after rotation. Use -1 or None for digits whose rotated pattern doesn't correspond to any valid digit.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem has two components that must both be satisfied:
-
Segment transformation: When a cell rotates 180°, its lit segments map according to the physical transformation rules (top↔bottom, left↔right, middle unchanged).
-
Position reversal: The entire display reverses, so the cell originally at position
kends up at positionn-1-k.
The key insight is to precompute which digit (if any) each original digit becomes after its segments are rotated. Then verify that for every position i, the digit currently there matches what should appear there after rotation.
Building the Rotation Mapping
For each digit 0-9, apply the segment transformation rules to its lit segments, then check if the resulting pattern matches any valid digit:
- 0 (abcdef) → (abcdef) = 0 ✓
- 1 (bc) → (ef) = invalid (left-side bar, not a recognized digit)
- 2 (abdeg) → (abdeg) = 2 ✓
- 3 (abcdg) → (adefg) = invalid
- 4 (bcfg) → (cefg) = invalid
- 5 (acdfg) → (acdfg) = 5 ✓
- 6 (acdefg) → (abcdfg) = 9 ✓
- 7 (abc) → (def) = invalid
- 8 (abcdefg) → (abcdefg) = 8 ✓
- 9 (abcdfg) → (acdefg) = 6 ✓
Only digits {0, 2, 5, 6, 8, 9} have valid rotations. Note that 1 is not valid here (unlike classic "strobogrammatic" problems) because the seven-segment representation of 1 is right-aligned and becomes left-aligned when rotated, which is not a valid digit pattern.
Verification Logic
After rotation, position i receives the cell that was originally at position n-1-i, with its segments transformed. The display is symmetric if:
rotation_map[digits[n-1-i]] == digits[i] for all i
If any digit maps to an invalid pattern (we use -1 as a sentinel), the comparison fails and we return False.
Correctness
This captures the exact requirement: each position must show the same digit before and after rotation. The rotation map encodes the per-cell transformation, and the reversed indexing handles the positional reversal. For odd-length displays, the middle cell must map to itself (so it must be 0, 2, 5, or 8), which the general check naturally enforces.
Complexity
- Time: O(n) where n is the number of digits. Building the rotation map is O(1) (constant work for 10 digits). The verification loop makes one lookup per digit.
- Space: O(1) extra space. The rotation map and segment patterns are fixed-size data structures independent of input size.
SEGMENTS = ["abcdef", "bc", "abdeg", "abcdg", "bcfg", "acdfg", "acdefg", "abc", "abcdefg", "abcdfg"]
SEGMENT_ROTATION = {"a": "d", "d": "a", "b": "e", "e": "b", "c": "f", "f": "c", "g": "g"}
def is_display_symmetric(digits: list[int]) -> bool:
# Build reverse lookup: segment pattern -> digit
pattern_to_digit = {frozenset(seg_str): digit for digit, seg_str in enumerate(SEGMENTS)}
# Build rotation map: digit -> rotated digit (or -1 if invalid)
rotation_map = []
for seg_str in SEGMENTS:
rotated_segments = frozenset(SEGMENT_ROTATION[ch] for ch in seg_str)
rotation_map.append(pattern_to_digit.get(rotated_segments, -1))
# Check if display is symmetric under rotation
n = len(digits)
for i in range(n):
rotated_digit = rotation_map[digits[n - 1 - i]]
if rotated_digit != digits[i]:
return False
return True