Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created December 25, 2023 01:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unilecs/26abc5bc60104341e2e4e48defeb898f to your computer and use it in GitHub Desktop.
Save unilecs/26abc5bc60104341e2e4e48defeb898f to your computer and use it in GitHub Desktop.
Задача: Пропущенная двоичная строка
using System;
using System.Collections.Generic;
public class Program
{
public static string ConvertNumToBinaryString(int num, int length)
{
string binary = Convert.ToString(num, 2);
// добавляем ведущие нули до размера length
// Например, length = 5, binary = 101 -> "00101"
if (binary.Length < length)
{
binary = string.Concat(System.Linq.Enumerable.Repeat("0", length - binary.Length)) + binary;
}
return binary;
}
public static string FindMissedBinaryString(string[] nums) {
var set = new HashSet<string>(nums);
var len = nums[0].Length;
int digitNum = (int)Math.Pow(2, len);
for (int i = 0; i < digitNum; i++)
{
string binary = ConvertNumToBinaryString(i, len);
if (!set.Contains(binary))
{
return binary;
}
}
return "";
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(FindMissedBinaryString(new string[] { "01", "10" })); // "00"
Console.WriteLine(FindMissedBinaryString(new string[] { "000", "111","011","001" })); // "010"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment