TopCoder problem "DirPattern" used in TC China 08 - 1C (Division I Level One)



Problem Statement

    

Your home directory contains a list of files. All the filenames have equal length. When you type a command in the form "dir PATTERN", it will show all the filenames that match the specified pattern. A pattern can contain only letters ('a'-'z'), '.' characters, and '?' symbols. Each '?' matches any single character (including '.'), and all other characters match only themselves. For example, the pattern "conte?t.info" matches the filenames "contest.info" and "content.info", but not "contemn.info" or "contests.nfo" (all quotes for clarity only).

You are given a String[] filenames, where each element represents the filename of a single file in your home directory. Return the pattern that will match all the filenames while containing as few '?' symbols as possible.

 

Definition

    
Class:DirPattern
Method:matchFiles
Parameters:String[]
Returns:String
Method signature:String matchFiles(String[] filenames)
(be sure your method is public)
    
 

Constraints

-filenames will contain between 1 and 50 elements, inclusive.
-Each element of filenames will contain between 1 and 50 characters, inclusive.
-All elements of filenames will have equal length.
-Each element of filenames will contain only lowercase letters ('a'-'z') and '.' characters.
 

Examples

0)
    
{"contest.txt", "context.txt"}
Returns: "conte?t.txt"
1)
    
{"config.sys", "config.inf", "configures"}
Returns: "config????"
2)
    
{"c.user.mike.programs", "c.user.nike.programs", "c.user.rice.programs"}
Returns: "c.user.?i?e.programs"
3)
    
{"a", "a", "b", "b"}
Returns: "?"
4)
    
{"onlyonefile"}
Returns: "onlyonefile"

Problem url:

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

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13675&pm=7305

Writer:

Mike Mirzayanov

Testers:

PabloGilberto , brett1479 , Yarin , Olexiy

Problem categories:

String Manipulation