Advertisement
dragonbs

Post Office

Mar 30th, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.RegularExpressions;
  4. using System.Linq;
  5.  
  6. internal class Program
  7. {
  8.     static void Main()
  9.     {
  10.         string[] input = Console.ReadLine().Split('|');
  11.         char[] capitalLetters = GetCapitalLetters(input[0]);
  12.         Dictionary<char, int> wordLengthsByFirstLetter = GetWordLengths(input[1], capitalLetters);
  13.         List<string> extractedWords = ExtractValidWords(input[2], wordLengthsByFirstLetter);
  14.  
  15.         for (int i = 0; i < capitalLetters.Length; i++)
  16.             Console.WriteLine(extractedWords.Find(x => x[0] == capitalLetters[i]));
  17.     }
  18.  
  19.     private static List<string> ExtractValidWords(string inputText, Dictionary<char, int> wordLengthsByFirstLetter)
  20.     {
  21.         string[] allWords = inputText.Split();
  22.         List<string> validWords = new List<string>();
  23.         foreach (string word in allWords)
  24.         {
  25.             char firstLetter = word[0];
  26.             if (wordLengthsByFirstLetter.ContainsKey(firstLetter)
  27.                 && wordLengthsByFirstLetter[firstLetter] == word.Length)
  28.             {
  29.                 validWords.Add(word);
  30.             }
  31.         }
  32.         return validWords;
  33.     }
  34.  
  35.     private static Dictionary<char, int> GetWordLengths(string inputText, char[] capitalLetters)
  36.     {
  37.         var wordLengthsByFirstLetter = new Dictionary<char, int>();
  38.  
  39.         string regex = @"(?<ascii>[\d]{2}):(?<length>[\d]{2})";
  40.         MatchCollection matches = Regex.Matches(inputText, regex);
  41.  
  42.         foreach (Match match in matches)
  43.         {
  44.             char asciiSymbol = (char)int.Parse(match.Groups["ascii"].Value);
  45.             int length = 1 + int.Parse(match.Groups["length"].Value);
  46.  
  47.             if (capitalLetters.Contains(asciiSymbol))
  48.             {
  49.                 if (wordLengthsByFirstLetter.ContainsKey(asciiSymbol))
  50.                     wordLengthsByFirstLetter[asciiSymbol] = length;
  51.                 else
  52.                     wordLengthsByFirstLetter.Add(asciiSymbol, length);
  53.             }
  54.         }
  55.         return wordLengthsByFirstLetter;
  56.     }
  57.  
  58.     private static char[] GetCapitalLetters(string inputText)
  59.     {
  60.         string regex = @"([#$%*&])(?<capitalChars>[A-Z]+)\1";
  61.         MatchCollection match = Regex.Matches(inputText, regex);
  62.         string regexResult = String.Empty;
  63.         for (int i = 0; i < match.Count; i++)
  64.         {
  65.             regexResult += match[i].Groups["capitalChars"].Value;
  66.         }
  67.  
  68.         return regexResult.ToCharArray();
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement