Notes on SRM 350, D2, 250-Pointer (DistanceBetweenStrings)
James S. Plank
Mon Jan 20 11:58:08 EST 2014
This is your first in-lab Topcoder problem, so I'm writing these lecture notes to help
walk you through solving it. First, read the problem description and make sure
that you understand what it's asking. Go through each example, and make sure that you
know why each answer is correct. With example 4, use math rather than
The answer is 5000, and the first string is all a's, while the second string is all b's.
And the strings are the same size. So, let that size be s. You know that the
distance between the two strings with respect to the character 'a' is s2.
distance between the two strings with respect to the character 'b' is also
s2. So, you need to find an s such that
2s2 equals 5000. That means s2 equals 2500.
So s is 50: the first string is 50 a's, and the second is 50 b's. Excellent.
Now, let's start programming a solution. Your first job is to set up the DistanceBetweenStrings class, which has a public method called getDistance(). getDistance()
should take three strings as parameters, and return an integer.
Now, implement getDistance() so that it simply returns zero. Get that to compile
in the Topcoder app and test it. You'll see that it gives the right answer for examples 1
and 5. Ha ha.
Now, update your program so that it goes through a and b and converts upper
case to lower case. Have it print out a and b, but still have it return 0.
It won't be correct, but you can now test whether you did it correctly by looking at the
output, especially for examples 3 and 5. Remember from class -- you can test whether a
character is uppercase by testing it against the characters 'A' and 'Z'. Then you can
convert uppercase to lowercase by subtracting 'A' and adding 'a'.
Now, go through every character in letterSet, and
count the occurrences of that character in a and in b.
Print that out. You should still return 0 -- you just want to make sure you have
your code running correctly. Here it is on all of the examples:
t 1 2
c 1 1
p 1 0
f 1 1
e 1 1
d 1 1
a 5 0
a 4 3
b 1 2
b 0 50
a 50 0
w 0 0
x 0 0
y 0 0
z 0 0
Now finish your program by calculating the distances and returning the total.