PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates understanding of character-to-character mappings in strings, modeling those mappings as directed graphs, detection and handling of cycles, and the ability to construct a sequence of replacements.

  • Medium
  • MathWorks
  • Coding & Algorithms
  • Software Engineer

Decide transform via one-to-one mapping

Company: MathWorks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Take-home Project

You are given two strings s and t of equal length over the lowercase English alphabet. In one step, you may pick any character x and replace all its occurrences in s with a single character y (the mapping x→y must be applied consistently across the entire string and persists for subsequent steps). Determine whether s can be transformed into t in finitely many steps. If cycles occur in the mapping, you may only break them if there exists at least one spare character not present in t to use as a temporary buffer. Design an algorithm to decide transformability and, if possible, output one valid sequence of replacements.

Quick Answer: This question evaluates understanding of character-to-character mappings in strings, modeling those mappings as directed graphs, detection and handling of cycles, and the ability to construct a sequence of replacements.

You are given two strings `s` and `t` of equal length over the lowercase English alphabet (`'a'`-`'z'`). In one step you may pick any character `x` and replace ALL of its current occurrences in `s` with a single character `y`. The mapping `x -> y` is applied consistently across the entire string and persists for subsequent steps. Determine whether `s` can be transformed into `t` in finitely many such steps. Return `true` if the transformation is possible, otherwise `false`. Key observations: - Every distinct character of `s` must map to exactly one character of `t` (if the same source character must become two different targets, it is impossible). - If `s` already equals `t`, zero steps are needed, so it is trivially possible. - If `s != t`, replacements may create cyclic dependencies (e.g. `a -> b` and `b -> a`). A cycle can only be broken if there is at least one spare character (one of the 26 letters that does NOT appear in `t`) to use as a temporary buffer. Hence when `s != t`, the transformation is possible if and only if `t` uses fewer than 26 distinct characters.

Constraints

  • 1 <= len(s) == len(t) <= 10^5 (the strings have equal length)
  • s and t consist only of lowercase English letters 'a'-'z'
  • Empty strings are considered equal and trivially transformable

Examples

Input: ("aabcc", "ccdee")

Expected Output: True

Explanation: Map a->c, b->d, c->e applied carefully (process in an order that avoids overwriting). t uses 3 distinct chars (< 26), so a spare buffer exists. Transformable.

Input: ("leetcode", "codeleet")

Expected Output: False

Explanation: Character 'e' in s would need to map to multiple different characters in t, violating the consistent one-to-one mapping requirement. Not transformable.

Input: ("", "")

Expected Output: True

Explanation: Two empty strings are equal, so zero steps are needed.

Input: ("a", "b")

Expected Output: True

Explanation: Single mapping a->b; t uses 1 distinct char, plenty of spare buffers. Transformable.

Input: ("ab", "ba")

Expected Output: True

Explanation: Requires the cycle a<->b. Since t = 'ba' uses only 2 of 26 letters, a spare character (e.g. 'c') can buffer the swap: a->c, b->a, c->b. Transformable.

Input: ("abcdefghijklmnopqrstuvwxyz", "bcdefghijklmnopqrstuvwxyza")

Expected Output: False

Explanation: A full 26-letter rotation forms one big cycle, but t uses all 26 letters so there is NO spare buffer character to break it. Not transformable.

Input: ("aa", "bc")

Expected Output: False

Explanation: The same source character 'a' would have to become both 'b' and 'c', which is impossible under a consistent mapping.

Hints

  1. First check that the mapping from s to t is a function: each distinct character in s must always correspond to the same character in t. If any source character needs two different targets, it is impossible.
  2. If s already equals t, the answer is immediately true (no steps required).
  3. When s != t, replacements can form cycles. You need a 'free' letter (one not used anywhere in t) to serve as a temporary holding spot. So the answer is true iff t has fewer than 26 distinct characters.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Minimize shortest path by adding weight-1 edges - MathWorks (easy)
  • Maximize minimum after K decrements - MathWorks (easy)
  • How to maximize rewards with exactly k tasks - MathWorks (easy)
  • Maximize minimum value after k decrements - MathWorks (medium)
  • Determine Whether P's Position Is Unique - MathWorks (medium)