Check Permutation

In this lesson, you will learn how to check if a string is a permutation of another string in Python.

In this lesson, we will consider how to determine if a given string is a permutation of another string.

Specifically, we want to solve the following problem:

Given two strings, write a function to determine if one is a permutation of the other.

Here is an example of strings that are permutations of each other:

is_permutation_1 = "google"
is_permutation_2 = "ooggle"

The strings below are not permutations of each other.

not_permutation_1 = "not"
not_permutation_2 = "top"

We will solve this problem in Python and analyze the time and space complexity of our approach.

Let’s begin!

Solution 1

Implementation

A permutation of a string will have the same number of each type of character as that string as it is just a rearrangement of the letters.

Let’s have a look at the code below where we make use of this property:

Get hands-on with 1400+ tech skills courses.