Skip to the content

The ring lock

Problem

On level 7 of the crypt, every chest has two rings of symbols: the inner one stays put and the outer one turns. It opens with the turn that leaves the most symbols facing their match.

Write a function that takes outer and inner, two strings of uppercase letters with the same length (they may be empty). Turning k places moves the first k symbols of outer to the end: "ABCD" turned 1 is "BCDA", and turned 3 is "DABC". For each turn from 0 to the length minus 1, count the positions where the turned ring and inner have the same symbol, and return the turn, a whole number, with the most matches. With "AABB" and "ABBA": turn 0 matches in 2, turn 1 ("ABBA") in 4, turn 2 in 2 and turn 3 in 0, so it returns 1. If two turns tie, return the smaller one; if none matches anything, or both are empty, return 0.

Examples

  • The example

    "AABB", "ABBA" → 1

  • Turn 2 matches them all

    "ABCDE", "CDEAB" → 2

  • Two turns tie

    "ABAB", "BABA" → 1

  • No turn matches

    "AB", "CD" → 0

Besides these, the challenge has hidden tests that are revealed when you submit your solution.

You start with this

Python

def best_turn(outer, inner):
    pass

JavaScript

function bestTurn(outer, inner) {
}
Solve this challenge

It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.

More O(n²) challenges

See all challenges →