Compare Strings Under Swaps and Global Renaming

Quick Overview

Compare Strings Under Swaps and Global Renaming. Two operations may be applied repeatedly to either lowercase string: swap any two positions, or choose two letters already present and exchange all their occurrences at once.

Compare Strings Under Swaps and Global Renaming

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Two operations may be applied repeatedly to either lowercase string: swap any two positions, or choose two letters already present and exchange all their occurrences at once. Return whether `word1` can be transformed into `word2` under these rules. ```cpp class Solution { public: bool closeStrings(string word1, string word2); }; ``` ### Examples **Example 1:** ```text word1 = "aabbc", word2 = "abbcc" Output: true Explanation: Both use a, b, c with frequency multiset {1,2,2}; exchange a and c globally, then rearrange positions. ``` **Example 2:** ```text word1 = "aab", word2 = "ccd" Output: false Explanation: Global exchanges among existing letters cannot introduce c or d into the first string. ``` ### Constraints - Each length is in `[1,10^5]`. - Both strings contain lowercase English letters only. ### Clarifying Questions The global exchange uses two existing letters, so it cannot introduce an absent letter. It exchanges all occurrences, not just a selected pair of positions. ```hint Inspect the structure Which characters may participate in each operation, and can a permitted sequence introduce a letter absent at the start? ``` ### Reasoning checkpoints - Prove both impossibility and constructibility under the two stated operations. - Distinguish swapping positions from exchanging all occurrences of two existing letters. ### Optional extensions Why would checking only the sorted frequency counts accept some impossible transformations?

Overview: Compare Strings Under Swaps and Global Renaming. Two operations may be applied repeatedly to either lowercase string: swap any two positions, or choose two letters already present and exchange all their occurrences at once.

Google logo
Google
Jun 4, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Two operations may be applied repeatedly to either lowercase string: swap any two positions, or choose two letters already present and exchange all their occurrences at once. Return whether word1 can be transformed into word2 under these rules.

class Solution {
public:
    bool closeStrings(string word1, string word2);
};

Examples

Example 1:

word1 = "aabbc", word2 = "abbcc"
Output: true
Explanation: Both use a, b, c with frequency multiset {1,2,2}; exchange a and c globally, then rearrange positions.

Example 2:

word1 = "aab", word2 = "ccd"
Output: false
Explanation: Global exchanges among existing letters cannot introduce c or d into the first string.

Constraints

  • Each length is in [1,10^5] .
  • Both strings contain lowercase English letters only.

Clarifying Questions Guidance

The global exchange uses two existing letters, so it cannot introduce an absent letter. It exchanges all occurrences, not just a selected pair of positions.

Reasoning checkpoints

  • Prove both impossibility and constructibility under the two stated operations.
  • Distinguish swapping positions from exchanging all occurrences of two existing letters.

Optional extensions

Why would checking only the sorted frequency counts accept some impossible transformations?