-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync LeetCode submission Runtime - 74 ms (75.00%), Memory - 3.7 MB (2…
…5.00%)
- Loading branch information
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
leetcode/3329-find-the-length-of-the-longest-common-prefix/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<p>You are given two arrays with <strong>positive</strong> integers <code>arr1</code> and <code>arr2</code>.</p> | ||
|
||
<p>A <strong>prefix</strong> of a positive integer is an integer formed by one or more of its digits, starting from its <strong>leftmost</strong> digit. For example, <code>123</code> is a prefix of the integer <code>12345</code>, while <code>234</code> is <strong>not</strong>.</p> | ||
|
||
<p>A <strong>common prefix</strong> of two integers <code>a</code> and <code>b</code> is an integer <code>c</code>, such that <code>c</code> is a prefix of both <code>a</code> and <code>b</code>. For example, <code>5655359</code> and <code>56554</code> have a common prefix <code>565</code> while <code>1223</code> and <code>43456</code> <strong>do not</strong> have a common prefix.</p> | ||
|
||
<p>You need to find the length of the <strong>longest common prefix</strong> between all pairs of integers <code>(x, y)</code> such that <code>x</code> belongs to <code>arr1</code> and <code>y</code> belongs to <code>arr2</code>.</p> | ||
|
||
<p>Return <em>the length of the <strong>longest</strong> common prefix among all pairs</em>.<em> If no common prefix exists among them</em>, <em>return</em> <code>0</code>.</p> | ||
|
||
<p> </p> | ||
<p><strong class="example">Example 1:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> arr1 = [1,10,100], arr2 = [1000] | ||
<strong>Output:</strong> 3 | ||
<strong>Explanation:</strong> There are 3 pairs (arr1[i], arr2[j]): | ||
- The longest common prefix of (1, 1000) is 1. | ||
- The longest common prefix of (10, 1000) is 10. | ||
- The longest common prefix of (100, 1000) is 100. | ||
The longest common prefix is 100 with a length of 3. | ||
</pre> | ||
|
||
<p><strong class="example">Example 2:</strong></p> | ||
|
||
<pre> | ||
<strong>Input:</strong> arr1 = [1,2,3], arr2 = [4,4,4] | ||
<strong>Output:</strong> 0 | ||
<strong>Explanation:</strong> There exists no common prefix for any pair (arr1[i], arr2[j]), hence we return 0. | ||
Note that common prefixes between elements of the same array do not count. | ||
</pre> | ||
|
||
<p> </p> | ||
<p><strong>Constraints:</strong></p> | ||
|
||
<ul> | ||
<li><code>1 <= arr1.length, arr2.length <= 5 * 10<sup>4</sup></code></li> | ||
<li><code>1 <= arr1[i], arr2[i] <= 10<sup>8</sup></code></li> | ||
</ul> |
28 changes: 28 additions & 0 deletions
28
leetcode/3329-find-the-length-of-the-longest-common-prefix/solution.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
impl Solution { | ||
pub fn longest_common_prefix(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 { | ||
use std::collections::HashSet; | ||
let mut s1 = HashSet::new(); | ||
let mut s2 = HashSet::new(); | ||
for mut x in arr1 { | ||
while x > 0 { | ||
s1.insert(x); | ||
x /= 10; | ||
} | ||
} | ||
for mut x in arr2 { | ||
while x > 0 { | ||
s2.insert(x); | ||
x /= 10; | ||
} | ||
} | ||
let mut ret = 0; | ||
if let Some(&x) = s1.intersection(&s2).max() { | ||
let mut x = x; | ||
while x > 0 { | ||
ret += 1; | ||
x /= 10; | ||
} | ||
} | ||
return ret; | ||
} | ||
} |