Talentd
FleetCode

3556. Sum of Largest Prime Substrings

Medium37.9% acceptance1 company

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:

Input: s = "12234"

Output: 1469

Explanation:

  • The unique prime numbers formed from the substrings of "12234" are 2, 3, 23, 223, and 1223.
  • The 3 largest primes are 1223, 223, and 23. Their sum is 1469.

Example 2:

Input: s = "111"

Output: 11

Explanation:

  • The unique prime number formed from the substrings of "111" is 11.
  • Since there is only one prime number, the sum is 11.

Constraints:

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

Asked at 1 company

Hints open one at a time

Hint 1
Iterate over all substrings of s to generate candidate numbers.
Hint 2
Check each candidate for primality in O(sqrt(n)) time.
Hint 3
Store unique primes, then sum the three largest (or all if fewer than three).
8:35
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def sumOfLargestPrimes(self, s: str) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
"12234"
Input
"111"
Your code is saved on this device.Solution