Sum of Largest Prime Substrings - Problem
Given a string s, find the sum of the 3 largest unique prime numbers that can be formed using any of its substrings.
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.
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.
Input & Output
Example 1 — Basic Case
$
Input:
s = "1237"
›
Output:
67
💡 Note:
Substrings: 1, 12, 123, 1237, 2, 23, 237, 3, 37, 7. Prime numbers: 2, 3, 7, 23, 37. Top 3 largest: 37, 23, 7. Sum: 37 + 23 + 7 = 67
Example 2 — With Leading Zeros
$
Input:
s = "0023"
›
Output:
28
💡 Note:
Substrings after converting (ignoring leading zeros): 0, 2, 23, 3. Prime numbers: 2, 3, 23. Top 3: 23 + 3 + 2 = 28
Example 3 — No Primes
$
Input:
s = "4689"
›
Output:
0
💡 Note:
All possible substrings result in composite numbers or 1. No prime numbers can be formed, so return 0
Constraints
- 1 ≤ s.length ≤ 1000
- s consists of digits only ('0'-'9')
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code