| | 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.FindTheLengthOfTheLongestCommonPrefix; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindTheLengthOfTheLongestCommonPrefixHashSet : IFindTheLengthOfTheLongestCommonPrefix |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(m * d1 + n * d2), where m is the number of elements in arr1, n is the number of elements |
| | 19 | | /// arr2, d1 is the average number of digits in arr1, and d2 is the average number of digits in arr2 |
| | 20 | | /// Space complexity - O(m * d1), where m is the number of elements in arr1, d1 is the average number of digits |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="arr1"></param> |
| | 23 | | /// <param name="arr2"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public int LongestCommonPrefix(int[] arr1, int[] arr2) |
| 7 | 26 | | { |
| 7 | 27 | | var arr1PrefixesHashSet = new HashSet<int>(); |
| | 28 | |
|
| 58 | 29 | | for (var i = 0; i < arr1.Length; i++) |
| 22 | 30 | | { |
| 125 | 31 | | while (arr1[i] > 0) |
| 103 | 32 | | { |
| 103 | 33 | | arr1PrefixesHashSet.Add(arr1[i]); |
| | 34 | |
|
| 103 | 35 | | arr1[i] /= 10; |
| 103 | 36 | | } |
| 22 | 37 | | } |
| | 38 | |
|
| 7 | 39 | | var longestPrefix = 0; |
| | 40 | |
|
| 56 | 41 | | for (var i = 0; i < arr2.Length; i++) |
| 21 | 42 | | { |
| 72 | 43 | | while (arr2[i] > 0) |
| 64 | 44 | | { |
| 64 | 45 | | if (arr1PrefixesHashSet.Contains(arr2[i])) |
| 13 | 46 | | { |
| 13 | 47 | | break; |
| | 48 | | } |
| | 49 | |
|
| 51 | 50 | | arr2[i] /= 10; |
| 51 | 51 | | } |
| | 52 | |
|
| 21 | 53 | | if (arr2[i] > 0) |
| 13 | 54 | | { |
| 13 | 55 | | longestPrefix = Math.Max(longestPrefix, (int)Math.Floor(Math.Log10(arr2[i])) + 1); |
| 13 | 56 | | } |
| 21 | 57 | | } |
| | 58 | |
|
| 7 | 59 | | return longestPrefix; |
| 7 | 60 | | } |
| | 61 | | } |