Skip to main content

Do Two Strings Contain the Same Characters?

Problem Description​

Write a function that accepts two String parameters and returns true if they contain the same characters in any order, taking into account letter case.

Input​

  • Two strings:
    1. String1
    2. String2

Output​

  • A boolean value: true if the two strings contain the same characters in any order, considering letter case; otherwise, false.

Constraints​

  • The comparison should take letter case into account.
  • Strings may contain alphanumeric characters and spaces.

Example​

Input:
"abca"
"abca"

Output:
true

Explanation:
Both strings contain the same characters 'a', 'b', 'c', and 'a' in the same frequency.

Input:
"abc"
"cba"

Output:
true

Explanation:
Both strings contain the same characters 'a', 'b', and 'c' in any order.

Input:
"a1 b2"
"b1 a2"

Output:
true

Explanation:
Both strings contain the same characters 'a', '1', ' ', 'b', and '2' in any order.

Input:
"abc"
"abca"

Output:
false

Explanation:
The first string does not contain as many 'a's as the second string.

Input:
"abc"
"Abc"

Output:
false

Explanation:
The comparison is case-sensitive, so 'a' and 'A' are considered different characters.

Input:
"abc"
"cbAa"

Output:
false

Explanation:
The first string does not contain as many characters as the second string, and the comparison is case-sensitive.