Sum of Largest Prime Substrings - Problem
You're a cryptographer tasked with finding the most valuable prime codes hidden within an encrypted message string!
Given a string s, your mission is to:
- Extract all possible substrings from the string
- Convert each substring to an integer (ignoring leading zeros)
- Identify which of these integers are prime numbers
- Find the 3 largest unique primes and return their sum
Special Rules:
- Each prime is counted only once, even if it appears multiple times
- Leading zeros are ignored when converting substrings to integers
- If fewer than 3 unique primes exist, return the sum of all available primes
- If no primes can be formed, return
0
Example: For string "2311", substrings include "2", "23", "231", "2311", "3", "31", "311", "1", "11", "1". The unique primes are: 2, 23, 3, 31, 311. The 3 largest are 311, 31, 23, so return 311 + 31 + 23 = 365.
Input & Output
example_1.py β Basic Case
$
Input:
s = "2311"
βΊ
Output:
365
π‘ Note:
Substrings: "2"(2), "23"(23), "231"(231), "2311"(2311), "3"(3), "31"(31), "311"(311), "1"(1), "11"(11), "1"(1). Primes found: 2, 23, 3, 31, 311 (231=3Γ7Γ11, 2311=non-prime, 1=non-prime, 11=prime but smaller). Unique primes sorted: [311, 31, 23, 11, 3, 2]. Top 3: 311+31+23=365.
example_2.py β With Leading Zeros
$
Input:
s = "0123"
βΊ
Output:
26
π‘ Note:
Substrings include "01"(1), "012"(12), "0123"(123), "12"(12), "123"(123), "23"(23), "3"(3), etc. After removing leading zeros: 1, 12, 123, 12, 123, 23, 3. Unique numbers: {1, 12, 123, 23, 3}. Primes: 23, 3. Only 2 primes found, so return 23+3=26.
example_3.py β No Primes
$
Input:
s = "1469"
βΊ
Output:
0
π‘ Note:
All possible substrings convert to composite numbers or 1. Numbers like 1(not prime), 4(2Β²), 6(2Γ3), 9(3Β²), 14(2Γ7), 46(2Γ23), 69(3Γ23), 146(2Γ73), 469(7Γ67), 1469(non-prime). No prime numbers can be formed, so return 0.
Constraints
- 1 β€ s.length β€ 1000
- s consists only of digits ('0' to '9')
- All numbers formed will fit in a 64-bit signed integer
- Leading zeros in substrings are ignored when converting to integers
- At most 106 substrings to check for primality
Visualization
Tap to expand
Understanding the Visualization
1
Map Exploration
Systematically explore all possible routes (substrings) on the treasure map
2
Coordinate Conversion
Convert each route into treasure coordinates (string to integer, handling leading zeros)
3
Treasure Verification
Use advanced detection equipment (optimized prime testing) to verify if coordinates contain real treasure
4
Unique Collection
Store verified treasures in a secure vault (hash set) to avoid counting duplicates
5
Value Assessment
Sort treasures by value and select the 3 most valuable ones
6
Final Reward
Calculate the total value of the top 3 treasures found
Key Takeaway
π― Key Insight: Use a hash set for automatic uniqueness and optimize prime checking with the 6kΒ±1 method to efficiently find the most valuable prime treasures hidden in the string!
π‘
Explanation
AI Ready
π‘ Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code