Created
January 19, 2026 13:49
-
-
Save enderahmetyurt/f49aa35877cdeb83f47a4c7e2fc21399 to your computer and use it in GitHub Desktop.
Given a string str, find a contiguous substring of length 10 whose characters can be bijectively mapped to the moves {U,D,L,R,B,A} so that the substring decodes to the Konami code "UUDDLRLRBA" (a character always maps to the same move, and two different moves can’t share a character). Return a valid mapping as an object.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def find_konami_mapping(str) | |
| konami = "UUDDLRLRBA" | |
| each_konami_window(str) do |substring| | |
| mapping = build_mapping(substring, konami) | |
| return mapping if mapping | |
| end | |
| nil | |
| end | |
| private | |
| def each_konami_window(str) | |
| konami_length = 10 | |
| return if str.length < konami_length | |
| (0..str.length - konami_length).each do |i| | |
| yield str[i, konami_length] | |
| end | |
| end | |
| def build_mapping(substring, konami) | |
| char_to_move = {} | |
| move_to_char = {} | |
| substring.chars.each_with_index do |char, index| | |
| move = konami[index] | |
| return nil if conflicts_with_existing_mapping?(char, move, char_to_move, move_to_char) | |
| char_to_move[char] = move | |
| move_to_char[move] = char | |
| end | |
| char_to_move.sort.to_h | |
| end | |
| def conflicts_with_existing_mapping?(char, move, char_to_move, move_to_char) | |
| (char_to_move[char] && char_to_move[char] != move) || | |
| (move_to_char[move] && move_to_char[move] != char) | |
| end | |
| puts find_konami_mapping("xx2233454590yy11110") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment