Check whether two strings match under character swaps and letter relabeling

Quick Overview

Decide whether one lowercase string can be turned into another by swapping characters and by exchanging all occurrences of two letters that both appear in it. The problem tests reasoning about which properties the allowed operations preserve and checking them in linear time with character counts.

Check whether two strings match under character swaps and letter relabeling

Company: Postmates

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Two strings are considered close if you can attain one from the other using the following operations: - Operation 1: swap any two existing characters. For example, `abcde -> aecdb`. - Operation 2: transform every occurrence of one existing character into another existing character, and do the same with the other character. For example, `aacabb -> bbcbaa` (all `a`s turn into `b`s, and all `b`s turn into `a`s). You can use the operations on either string as many times as necessary. Given two strings, `word1` and `word2`, return `True` if `word1` and `word2` are close, and `False` otherwise. ### Function Signature ```python def close_strings(word1: str, word2: str) -> bool: ... ``` ### Rules - Operation 1 swaps the characters at any two positions of the string. - Operation 2 picks two letters that both appear in the string at the moment it is applied and exchanges all of their occurrences at once. It cannot turn a letter into one that does not appear in the string. - Operations may be applied to `word1`, to `word2`, or to both, in any order and any number of times, including zero. ### Constraints - `1 <= len(word1), len(word2) <= 10^5` - `word1` and `word2` contain only lowercase English letters. ### Examples ```text Input: word1 = "abc", word2 = "bca" Output: True ``` Operation 1 alone is enough: swapping the first two characters gives "bac", and swapping the last two gives "bca". ```text Input: word1 = "a", word2 = "aa" Output: False ``` The strings have different lengths, and neither operation changes the length of a string, so no sequence of operations can transform one into the other. ```text Input: word1 = "cabbba", word2 = "abbccc" Output: True ``` "cabbba" has one `c`, two `a`s and three `b`s. Exchanging `a` and `b` gives one `c`, two `b`s and three `a`s, and then exchanging `a` and `c` gives one `a`, two `b`s and three `c`s. Swaps then rearrange the characters into "abbccc".

Overview: Decide whether one lowercase string can be turned into another by swapping characters and by exchanging all occurrences of two letters that both appear in it. The problem tests reasoning about which properties the allowed operations preserve and checking them in linear time with character counts.

Postmates logo
Postmates
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Two strings are considered close if you can attain one from the other using the following operations:

  • Operation 1: swap any two existing characters. For example, abcde -> aecdb .
  • Operation 2: transform every occurrence of one existing character into another existing character, and do the same with the other character. For example, aacabb -> bbcbaa (all a s turn into b s, and all b s turn into a s).

You can use the operations on either string as many times as necessary.

Given two strings, word1 and word2, return True if word1 and word2 are close, and False otherwise.

Function Signature

def close_strings(word1: str, word2: str) -> bool:
    ...

Rules

  • Operation 1 swaps the characters at any two positions of the string.
  • Operation 2 picks two letters that both appear in the string at the moment it is applied and exchanges all of their occurrences at once. It cannot turn a letter into one that does not appear in the string.
  • Operations may be applied to word1 , to word2 , or to both, in any order and any number of times, including zero.

Constraints

  • 1 <= len(word1), len(word2) <= 10^5
  • word1 and word2 contain only lowercase English letters.

Examples

Input: word1 = "abc", word2 = "bca"
Output: True

Operation 1 alone is enough: swapping the first two characters gives "bac", and swapping the last two gives "bca".

Input: word1 = "a", word2 = "aa"
Output: False

The strings have different lengths, and neither operation changes the length of a string, so no sequence of operations can transform one into the other.

Input: word1 = "cabbba", word2 = "abbccc"
Output: True

"cabbba" has one c, two as and three bs. Exchanging a and b gives one c, two bs and three as, and then exchanging a and c gives one a, two bs and three cs. Swaps then rearrange the characters into "abbccc".