#54. 首字符匹配交换
首字符匹配交换
First Character Match and Swap
Background
Congcong is studying string processing. He encountered an interesting problem that requires special operations on an array of strings.
Problem Description
We'll say that 2 strings "match" if they are non-empty and their first chars are the same. Loop over and then return the given array of non-empty strings as follows: if a string matches an earlier string in the array, swap the 2 strings in the array. A particular first char can only cause 1 swap, so once a char has caused a swap, its later swaps are disabled. Using a map, this can be solved making just one pass over the array. More difficult than it looks.
Input Format
Input is given from standard input in the following format.
A single line containing multiple non-empty strings, separated by spaces.
Output Format
Output is printed to standard output in the following format.
Output a string representation of an array, where strings are enclosed in double quotes, elements are separated by commas and spaces, and the entire array is enclosed in square brackets.
Sample
"ab" "ac"
["ac\