Advertisement
Artem_Chepurov

Ex 1.2

Jun 15th, 2022
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace SandBox
  5. {
  6.     public class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string guessedWord = Console.ReadLine();
  11.             string attempt = Console.ReadLine();
  12.  
  13.             if (attempt != null && guessedWord != null)
  14.             {
  15.                 int N = guessedWord.Length;
  16.                 HashSet<int> lettersUsedNumbers = new HashSet<int>();
  17.                 string[] answer = new string[N];
  18.                 Dictionary<char, int> countLetters = new Dictionary<char, int>();
  19.  
  20.                 for (int i = 0; i < N; i++)
  21.                 {
  22.                     if (countLetters.ContainsKey(guessedWord[i]))
  23.                     {
  24.                         countLetters[guessedWord[i]]++;
  25.                     }
  26.                     else
  27.                     {
  28.                         countLetters.Add(guessedWord[i], 1);
  29.                     }
  30.                     if (guessedWord[i] == attempt[i])
  31.                     {
  32.                         answer[i] = "correct";
  33.                         countLetters[guessedWord[i]]--;
  34.                     }
  35.                 }
  36.  
  37.                 for (int i = 0; i < N; i++)
  38.                 {
  39.                     if (guessedWord[i] != attempt[i])
  40.                     {
  41.                         if (countLetters.ContainsKey(attempt[i]) && countLetters[attempt[i]] > 0 && guessedWord[i] != attempt[i])
  42.                         {
  43.                             answer[i] = "present";
  44.                             countLetters[attempt[i]]--;
  45.                         }
  46.                         else
  47.                         {
  48.                             answer[i] = "absent";
  49.                         }
  50.  
  51.                     }
  52.  
  53.                 }
  54.  
  55.                 for (int i = 0; i < N; i++)
  56.                 {
  57.                     Console.WriteLine(answer[i]);
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement