Problem Statement
Sometimes wireless devices can receive messages which are intended for other devices. Therefore, each device must check all received messages and process only the ones that are intended for it.
To enable this, each device has a unique key. Each message starts with either the key of some device or a special commonKey. Messages that start with commonKey must be processed by all devices. Given Strings key and commonKey, and a String[] data, containing all messages received by a device, find all of the messages which must be processed by this device. Remove the first occurrence of key or commonKey in each of these messages, and return them in the same order they appear in input.
To enable this, each device has a unique key. Each message starts with either the key of some device or a special commonKey. Messages that start with commonKey must be processed by all devices. Given Strings key and commonKey, and a String[] data, containing all messages received by a device, find all of the messages which must be processed by this device. Remove the first occurrence of key or commonKey in each of these messages, and return them in the same order they appear in input.
Definition
- Class:
- Message
- Method:
- readMessage
- Parameters:
- String[], String, String
- Returns:
- String[]
- Method signature:
- String[] readMessage(String[] data, String key, String commonKey)
- (be sure your method is public)
Constraints
- key and commonKey will each contain between 3 and 20 characters, inclusive.
- data will contain between 0 and 50 elements, inclusive.
- Each element of data will contain between 0 and 50 characters, inclusive.
- key and commonKey will contain only lowercase letters ('a'-'z').
- All elements of data will contain only spaces and lowercase letters ('a'-'z').
- key will not be a proper prefix of commonKey, and vice versa, but key can be equal to commonKey.
Examples
{"keyhello coder", "commonkeyhello world"}"key"
"commonkey"
Returns: {"hello coder", "hello world" }{"keyhello coder", "commonkeyhello world"}"key"
"key"
Returns: {"hello coder" }{}"key"
"commonkey"
Returns: { }{"keykeykey", "commonkey", "keycommonkey", "commoncommoncommonkey"}"key"
"common"
Returns: {"keykey", "key", "commonkey", "commoncommonkey" }You should remove only the first occurrence of key or commonKey from each message.