You are given a string that consists of two character arrays and a command. Your task is to create a new array from the given two by executing the command.
If the command says "join" it means that you should create an array with elements that are contained in both arrays. If the command says "right exclude" it means that the newly created array should contain only elements from the first array that are not contained in the second array. If the command says "left exclude" it means that you should create an array with elements from the second array that are not contained in the first array.
The newly created array should have its elements sorted by their ASCII code. Examples:
- You are given the array "ABCD", the array "CAFG" and the command "join". The new array should be "AC".
- You are given the array "ABCD", the array "CAFG" and the command "left exclude". The new array should be "FG".
- You are given the array "ABCD", the array "CAFG" and the command "right exclude". The new array should be "BD".
Input
The input data should be read from the console.
- A single line containing the two arrays and the command, separated by a '\' sign.
The input data will always be valid and in the format described. There is no need to check it explicitly.
Output
The output should be printed on the console. It should consist of exactly 1 line:
- The output should be the elements of the newly formed array, sorted by their ASCII code.
Constraints
- The characters of the arrays will be characters from the ASCII table.
- Each element in an array will have only one occurrence.
- Allowed working time for your program: 0.1 seconds.
- Allowed memory: 16 MB.
Examples
Here's my solution! You can use new List and it's function of Contains and not Contains (!Contains) (by using System Linq) - lines: 21, 32 and 43. Then add the new elements in this new list:
using System; using System.Collections.Generic; using System.Linq; class ArrayMatcher { static void Main() { string[] input = Console.ReadLine().Split('\\'); char[] firstArr = input[0].ToCharArray(); char[] secondArr = input[1].ToCharArray(); string command = input[2]; List<char> newArr = new List<char>(); if (command == "join") { for (int i = 0; i < firstArr.Length; i++) { if (secondArr.Contains(firstArr[i])) { newArr.Add(firstArr[i]); } } } else if (command == "right exclude") { for (int i = 0; i < firstArr.Length; i++) { if (!secondArr.Contains(firstArr[i])) { newArr.Add(firstArr[i]); } } } else if (command == "left exclude") { for (int i = 0; i < secondArr.Length; i++) { if (!firstArr.Contains(secondArr[i])) { newArr.Add(secondArr[i]); } } } newArr.Sort(); foreach (var VARIABLE in newArr) { Console.Write(VARIABLE); } Console.WriteLine(); } }