| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.RansomNote; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class RansomNoteCounting : IRansomNote |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m + n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="ransomNote"></param> |
| | 22 | | /// <param name="magazine"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public bool CanConstruct(string ransomNote, string magazine) |
| 3 | 25 | | { |
| 3 | 26 | | var magazineChars = new int['z' - 'a']; |
| | 27 | |
|
| 21 | 28 | | foreach (var magazineChar in magazine) |
| 6 | 29 | | { |
| 6 | 30 | | magazineChars[magazineChar - 'a']++; |
| 6 | 31 | | } |
| | 32 | |
|
| 22 | 33 | | foreach (var magazineChar in ransomNote.Select(ransomNoteChar => ransomNoteChar - 'a')) |
| 5 | 34 | | { |
| 5 | 35 | | if (magazineChars[magazineChar] > 0) |
| 3 | 36 | | { |
| 3 | 37 | | magazineChars[magazineChar]--; |
| 3 | 38 | | } |
| | 39 | | else |
| 2 | 40 | | { |
| 2 | 41 | | return false; |
| | 42 | | } |
| 3 | 43 | | } |
| | 44 | |
|
| 1 | 45 | | return true; |
| 3 | 46 | | } |
| | 47 | | } |