Sum of Largest Prime Substrings

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.

Example 1
Inputs = "12234"
Output1469
The unique prime numbers formed from the substrings of "12234" are 2, 3, 23, 223, and 1223, and the 3 largest primes are 1223, 223, and 23 with sum 1469.
Example 2
Inputs = "111"
Output11
The unique prime number formed from the substrings of "111" is 11, so the sum is 11.

Constraints

  • 1 <= s.length <= 10
  • s consists of only digits.

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate