Skip to the content

The coded telegram

Problem

Torreón, 1914. Leonor Garza, a telegraph operator, copies an enemy telegram written in code. She suspects it hides the name of a town: could that name have become the code through a simple letter swap?

Write a function that takes plain and code, two strings of lowercase letters with no spaces and of the same length (they may be empty). Each plain letter is swapped for the code letter in the same position.

Return true if the same plain letter always gives the same code letter and two different letters never give the same one; a letter may stay as it is. Otherwise return false. With both empty, true.

With "torreon" and "kbiixbf": t becomes k, o becomes b, r becomes i, e becomes x and n becomes f; both r give i and both o give b: true.

With "villa" and "xqlmm" it is false: the same l gives l, then m. With "paso" and "mama" too: p and s, different, both give m.

Examples

  • The example that works

    "torreon", "kbiixbf" → true

  • One letter with two swaps

    "villa", "xqlmm" → false

  • Two letters with the same swap

    "paso", "mama" → false

  • Not the other way around either

    "mama", "paso" → false

  • A letter that stays the same

    "a", "a" → true

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

You start with this

Python

def can_swap(plain, code):
    pass

JavaScript

function canSwap(plain, code) {
}
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 →