TopCoder problem "PostOffice" used in TCHS SRM 21 (Division I Level One)



Problem Statement

    

A new device is being designed for all the post offices in Byteland. This device will make mail classification and delivery more efficient by automatically recognizing the addresses written on envelopes. You are responsible for the software component that determines if two addresses match.

Given two Strings address1 and address2, determine if they match using the following procedure. First, remove all spaces from both addresses. Then, perform a case-insensitive string comparison. If the two strings are equal, return -1. Otherwise, return the leftmost 0-based position at which they differ (the index after removing the spaces).

 

Definition

    
Class:PostOffice
Method:matchAddress
Parameters:String, String
Returns:int
Method signature:int matchAddress(String address1, String address2)
(be sure your method is public)
    
 

Constraints

-address1 and address2 will each contain between 0 and 50 characters, inclusive.
-address1 and address2 will contain only letters ('a'-'z', 'A'-'Z'), digits ('0'-'9'), and spaces.
 

Examples

0)
    
"145 West 44th Street"
"145 west 44 th street"
Returns: -1
After all spaces are removed, the two addresses become:
"145West44thStreet"
"145west44thstreet"
These two strings are equal if you do a case-insensitive comparison, so -1 is returned.
1)
    
" wall street "
"WallStreet"
Returns: -1
Again, these two addresses are equal after all spaces are removed and a case-insensitive comparison is done between the resulting strings.
2)
    
" Wall Street"
"  Waal Street"
Returns: 2
First, remove the spaces:
"WallStreet"
"WaalStreet"
These strings differ at position 2 (0-based). The first string has an 'l' at that position while the second string has an 'a'.
3)
    
"                                                 A"
"a                                                 "
Returns: -1
4)
    
"Wall"
"WallStreet"
Returns: 4
These strings differ at position 4 (0-based). The first string does not have a character at that position while the second string has a 'S'.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6794

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10073&pm=6794

Writer:

zhuzeyuan

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

String Manipulation