DistanceBetweenStrings
SRM 350 · 2007-05-23 · by Xixas
Problem Statement
You will be given threeString s a, b and letterSet. All the letters in letterSet will be distinct. Return the distance between a and b with respect to letterSet.
Constraints
- a will contain between 1 and 50 characters, inclusive.
- a will contain only letters ('a'-'z', 'A'-'Z').
- b will contain between 1 and 50 characters, inclusive.
- b will contain only letters ('a'-'z', 'A'-'Z').
- letterSet will contain between 1 and 26 characters, inclusive.
- letterSet will contain only lowercase letters ('a'-'z').
- Each character in letterSet will be distinct.
"topcoder" "contest" "tcp" Returns: 2
The letter 't' occurs once in the first string and twice in the second; the letter 'c' occurs once in the first string and once in the second; the letter 'p' occurs once in the first string and does not occur in the second one. The total distance is 1+0+1=2.
"abcdef" "fedcba" "fed" Returns: 0
The second string is a permutation of the letters in the first one thus the distance is 0.
"aaaaa" "bbbbb" "a" Returns: 25
The distance with respect to a single character in this case is 5*5=25.
"aaAaB" "BbaAa" "ab" Returns: 2
Lowercase and uppercase letters are regarded the same.
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" "ba" Returns: 5000
These two strings are quite far away.
Submissions are judged against all 134 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class DistanceBetweenStrings with a public method int getDistance(string a, string b, string letterSet) · 134 test cases · 2 s / 256 MB per case