Swift Programming: Check if 2 Strings Contain the Same Characters

Efficiently verify if two strings contain the same characters

Arc Sosangyo
2 min readJun 3, 2024

You’re likely to encounter this type of problem in your work or during a coding exam:

Create a function that takes two string parameters and returns true if they consist of the same characters, regardless of their order and case sensitivity.

Here’s a sample input and what should be its corresponding output:

  • “swift” and “swift” returns true.
  • “swift” and “tfiws” returns true.
  • “swift” and “iftws” returns true.
  • “swift programming” and “programming swift” returns true.
  • “swift” and “swift programming” returns false.
  • “Swift” and “swift” returns false.
  • “swift” and “iftwss” returns false.

Let’s break it down to simplify solving it:

  • The first step is to create two parameters to store the first and second strings.
  • Since we will be checking each character in a string, let’s transform both the first and second parameters into arrays.
  • By sorting both transformed arrays, we can easily compare them to determine if they…

--

--

Arc Sosangyo

Father | Husband | Self-help Author | Coder | Traveler