3556. Sum of Largest Prime Substrings Medium

@problem@discussion
#Hash Table#Math#String#Sorting#Number Theory



1/**
2 * [3556] Sum of Largest Prime Substrings
3 *
4 * <p data-end="157" data-start="30">Given a string s, find the sum of the 3 largest unique <span data-keyword="prime-number">prime numbers</span> that can be formed using any of its <span data-keyword="substring">substrings</span>.
5 * <p data-end="269" data-start="166">Return the sum of the three largest unique prime numbers that can be formed. If fewer than three exist, return the sum of all available primes. If no prime numbers can be formed, return 0.
6 * <p data-end="370" data-is-last-node="" data-is-only-node="" data-start="271"><strong data-end="280" data-start="271">Note: Each prime number should be counted only once, even if it appears in multiple substrings. Additionally, when converting a substring to an integer, any leading zeros are ignored.
7 *  
8 * <strong class="example">Example 1:
9 * <div class="example-block">
10 * Input: <span class="example-io">s = "12234"</span>
11 * Output: <span class="example-io">1469</span>
12 * Explanation:
13 * 
14 * 	<li data-end="136" data-start="16">The unique prime numbers formed from the substrings of "12234" are 2, 3, 23, 223, and 1223.
15 * 	<li data-end="226" data-start="137">The 3 largest primes are 1223, 223, and 23. Their sum is 1469.
16 * </div>
17 * <strong class="example">Example 2:
18 * <div class="example-block">
19 * Input: <span class="example-io">s = "111"</span>
20 * Output: <span class="example-io">11</span>
21 * Explanation:
22 * 
23 * 	<li data-end="339" data-start="244">The unique prime number formed from the substrings of "111" is 11.
24 * 	<li data-end="412" data-is-last-node="" data-start="340">Since there is only one prime number, the sum is 11.
25 * </div>
26 *  
27 * Constraints:
28 * 
29 * 	<li data-end="39" data-start="18">1 <= s.length <= 10
30 * 	<li data-end="68" data-is-last-node="" data-start="40">s consists of only digits.
31 * 
32 */
33pub struct Solution {}
34
35// problem: https://leetcode.com/problems/sum-of-largest-prime-substrings/
36// discuss: https://leetcode.com/problems/sum-of-largest-prime-substrings/discuss/?currentPage=1&orderBy=most_votes&query=
37
38// submission codes start here
39
40impl Solution {
41    pub fn sum_of_largest_primes(s: String) -> i64 {
42        
43    }
44}
45
46// submission codes end
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_3556() {
54    }
55}
56

Back
© 2026 bowen.ge All Rights Reserved.