Given is a s colors string. Each color is separated by space and end with integer number, which represents their order. Return a new string with sorted order of colors.
Example:
Input: "red3 blue2 green4 white1"
Output: "white blue red green"
Solutions
classMyClass{publicclassLastCharComparer:IComparer<string> {publicintCompare(string? x,string? y) {if (x isnull&& y isnull) {return0; }if (x isnull|| y isnull) {return x isnull?-1:1; }var first =x[^1];var second =y[^1];returnfirst.CompareTo(second); } }publicstringColorsNameString(string str) {var colors =str.Split(' ');var orderedColors =newSortedSet<string>(newLastCharComparer());StringBuilder sb =new();foreach (var item in colors) {orderedColors.Add(item); }foreach (var item in orderedColors) {sb.Append(item[0..(item.Length-1)]);sb.Append(" "); }returnsb.ToString(); }}