Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Permutation Check for Two Strings

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Permutation Check for Two Strings. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

A PlayStation system may compare two user-entered strings, such as a profile label and a reordered version of it. Given two strings, determine whether one is a permutation of the other.

Two strings are permutations if they contain exactly the same characters with the same frequencies, regardless of order. The comparison is case-sensitive, and spaces and punctuation count as characters.

Formal Specification

Implement are_permutations(s, t):

  • Input: two strings s and t.
  • Output: return True if t is a permutation of s; otherwise return False.
  • Do not modify either input string.

Aim for an O(n) solution using a character-frequency data structure rather than sorting. Let n be the length of the longer input string.

Constraints

  • 0 <= len(s), len(t) <= 10^5
  • Strings may contain Unicode characters
  • Comparisons are case-sensitive
  • Whitespace and punctuation are significant

Function Signature

def are_permutations(s, t):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output