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
πŸ—ΊοΈ Prime Treasure Hunt: "2311"πŸ“ Treasure Map (Input String)" 2 3 1 1 "πŸ” Route Exploration (Substring Generation)"2"β†’ 2 βœ“"23"β†’ 23 βœ“"231"β†’ 231 βœ—"31"β†’ 31 βœ“"311"β†’ 311 βœ“πŸ’Ž Treasure Vault (Hash Set)Unique Prime Treasures Found: {2, 3, 23, 31, 311}Automatic duplicate removal β€’ O(1) insertion timeπŸ† Final Treasure AssessmentSorted by Value: [311, 31, 23, 3, 2]Top 3 Most Valuable: 311 + 31 + 23 = 365 gold coinsπŸ’°
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!
Asked in
Google 42 Amazon 38 Microsoft 35 Meta 28
43.7K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen