TopCoder problem "TwoWords" used in TCHS SRM 42 (Division I Level Two)



Problem Statement

    

Recently, some very old messages were discovered. Unfortunately, some of the letters are no longer legible. You are given a String statement containing one of the messages. Each illegible letter is represented with a '?' character. You know that all of the letters in the original text were lowercase ('a' - 'z'). For example, if the statement is "d??r", the original message might have been "door" or "dear", but not "ddr" (one letter is missing), "doctor" (too many letters) or "rear" (the first letter is wrong).



You are given two Strings word1 and word2, in the same format as statement. You must determine whether it's possible for word1 and word2 to both exist in the original message without overlapping. For example, the message "yellowtaxi" contains both "yell" and "tax", but not "yell" and "low". Return "both" if both words can exist without overlapping, "none" if neither word can exist, or "one" if at most one word can exist without overlapping (all quotes for clarity). See examples for further clarification.

 

Definition

    
Class:TwoWords
Method:howMany
Parameters:String, String, String
Returns:String
Method signature:String howMany(String statement, String word1, String word2)
(be sure your method is public)
    
 

Constraints

-statement, word1 and word2 will each contain between 1 and 50 characters, inclusive.
-statement, word1 and word2 will each contain only lowercase letters ('a'-'z') and question marks ('?').
 

Examples

0)
    
"yello??taxi"
"yellow"
"taxi"
Returns: "both"
The original message could have been "yellowataxi". Therefore, both words can exist without overlapping.
1)
    
"?ellowtaxi"
"yell"
"l?w"
Returns: "one"
If the statement was interpreted as "yellowtaxi", word1 could exist. If word2 was interpreted as "low", it could exist. However, only one of them can exist without overlapping.
2)
    
"tha?isunbelievable"
"han?y"
"?th?"
Returns: "none"
For all interpretations of the illegible characters, neither word can exist.
3)
    
"secondfirst"
"second"
"first"
Returns: "both"
word2 can come before word1.
4)
    
"me?sa?e"
"?ceage"
"essay"
Returns: "one"
5)
    
"?"
"??"
"???"
Returns: "none"
6)
    
"hello"
"h?"
"ll?"
Returns: "both"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10790&pm=7855

Writer:

mohamedafattah

Testers:

PabloGilberto , brett1479 , Olexiy , ivan_metelsky

Problem categories:

Brute Force, String Manipulation