Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Two systems store the same identifier, but one of them shuffled the characters and changed the format. Before a closer check, a job tests whether the two strings are made of exactly the same letters and digits. This is called an anagram test.
Write same_letters(first, second). Return True if the two strings have the same letters and digits, the same number of times each. Ignore order, capital letters, and anything that is not a letter or digit.
Function to write
same_letters(first: str, second: str) -> boolTrue when both strings have the same letters and digits the same number of times, after ignoring case and other characters.
How to approach it
Clean each string first: lowercase it and keep only letters and digits. Then compare how many times each character appears.
Sample cases
+ 3 held back until you submit
a shuffled id
Both are made of a, b, c, 1, 2 and 3, so the answer is True.
Input
Argument 1
'a1b2c3'Argument 2
'3c2b1a'Returns
Truedifferent counts of a letter
The first has two `a` and one `b`. The second has one `a` and two `b`. The answer is False.
Input
Argument 1
'aab'Argument 2
'abb'Returns
Falsecapital letters and spaces
Ignoring case and spaces, both are `dataengineer`, so the answer is True.
Input
Argument 1
'Data Engineer'Argument 2
'reenigne atad'Returns
Truetwo empty strings
Two empty strings have the same (no) letters, so the answer is True.
Input
Argument 1
''Argument 2
''Returns
TrueConstraints
A and a are the same letter.aab and abb are different.True or False.Worked example
Take 'Data Engineer' and 'reenigne atad'. For each string, keep only the letters and digits and lowercase them: dataengineer for both. Count the letters: d 1, a 3, t 1, e 3, n 2, g 1, i 1, r 1 in both strings. The two counts are equal, so the answer is True.
The trap: comparing the raw strings sorted treats the capital D, the space and the different punctuation as real differences, and returns False.
What this tests
Letter counting for an anagram check, the idea behind 'Valid Anagram'. Two strings are anagrams when their counts match. The extra step here is normalising first: decide what counts as a letter before you count. Data engineers meet the same step in matching names and IDs from two systems.
same_letters(first: str, second: str) -> boolSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.