| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 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 sealed class FindTheLengthOfTheLongestCommonPrefixTrie : IFindTheLengthOfTheLongestCommonPrefix |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O((n + m) * d), where n is the number of elements in arr1, m is the number of elements in |
| | | 19 | | /// arr2, d is the average number of digits in the numbers |
| | | 20 | | /// Space complexity - O(n * d), where n is the number of elements in arr1, d is the average number of digits in |
| | | 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 trie = new Trie(arr1); |
| | | 28 | | |
| | 7 | 29 | | return arr2.Select(trie.FindLongestPrefix).Prepend(0).Max(); |
| | 7 | 30 | | } |
| | | 31 | | |
| | | 32 | | private class TrieNode |
| | | 33 | | { |
| | 518 | 34 | | public TrieNode?[] Children { get; } = new TrieNode[10]; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | private class Trie |
| | | 38 | | { |
| | 7 | 39 | | private readonly TrieNode _root = new(); |
| | | 40 | | |
| | 7 | 41 | | public Trie(IEnumerable<int> nums) |
| | 7 | 42 | | { |
| | 7 | 43 | | AddRange(nums); |
| | 7 | 44 | | } |
| | | 45 | | |
| | | 46 | | private void AddRange(IEnumerable<int> nums) |
| | 7 | 47 | | { |
| | 65 | 48 | | foreach (var num in nums) |
| | 22 | 49 | | { |
| | 22 | 50 | | Add(num); |
| | 22 | 51 | | } |
| | 7 | 52 | | } |
| | | 53 | | |
| | | 54 | | private void Add(int num) |
| | 22 | 55 | | { |
| | 22 | 56 | | var node = _root; |
| | 22 | 57 | | var numStr = num.ToString(); |
| | | 58 | | |
| | 375 | 59 | | foreach (var idx in numStr.Select(digit => digit - '0')) |
| | 103 | 60 | | { |
| | 103 | 61 | | if (node?.Children[idx] == null) |
| | 99 | 62 | | { |
| | 99 | 63 | | if (node != null) |
| | 99 | 64 | | { |
| | 99 | 65 | | node.Children[idx] = new TrieNode(); |
| | 99 | 66 | | } |
| | 99 | 67 | | } |
| | | 68 | | |
| | 103 | 69 | | node = node?.Children[idx]; |
| | 103 | 70 | | } |
| | 22 | 71 | | } |
| | | 72 | | |
| | | 73 | | public int FindLongestPrefix(int num) |
| | 21 | 74 | | { |
| | 21 | 75 | | var node = _root; |
| | 21 | 76 | | var numStr = num.ToString(); |
| | 21 | 77 | | var longestPrefix = 0; |
| | | 78 | | |
| | 231 | 79 | | foreach (var idx in numStr.Select(digit => digit - '0')) |
| | 61 | 80 | | { |
| | 61 | 81 | | if (node?.Children[idx] != null) |
| | 46 | 82 | | { |
| | 46 | 83 | | longestPrefix++; |
| | | 84 | | |
| | 46 | 85 | | node = node.Children[idx]; |
| | 46 | 86 | | } |
| | | 87 | | else |
| | 15 | 88 | | { |
| | 15 | 89 | | break; |
| | | 90 | | } |
| | 46 | 91 | | } |
| | | 92 | | |
| | 21 | 93 | | return longestPrefix; |
| | 21 | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |