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
{"contest.txt", "context.txt"}Returns: "conte?t.txt"
{"config.sys", "config.inf", "configures"}Returns: "config????"
{"c.user.mike.programs", "c.user.nike.programs", "c.user.rice.programs"}Returns: "c.user.?i?e.programs"
{"a", "a", "b", "b"}Returns: "?"
{"onlyonefile"}Returns: "onlyonefile"