08. Sum-String

βœ… GFG solution to the Sum String problem: check if a numeric string follows a sum sequence using string addition and recursion. πŸš€

The problem can be found at the following link: πŸ”— Question Link

🧩 Problem Description

Given a digit string s, determine whether it can be split into more than two non-empty parts such that:

  1. The rightmost part equals the integer sum of the two parts immediately before it.

  2. This sum-rule applies recursively to the preceding parts.

  3. No part may have leading zeros unless it is exactly "0".

πŸ“˜ Examples

Example 1:


Input: s = "12243660"
Output: true
Explanation:
Split as ["12","24","36","60"]:
36 = 12 + 24
60 = 24 + 36

Example 2:

Example 3:

πŸ”’ Constraints:

  • 1 ≀ s.length() ≀ 100

  • s consists only of digits '0'–'9'.

βœ… My Approach

πŸ’‘ Idea

  1. Try every possible pair of lengths for the first two numbers (a,b).

  2. Skip any part that has a leading zero (length >1 and starts with '0').

  3. Recursively check that each subsequent substring equals the string-sum of the previous two:

    • Use a helper addStrings(x,y) to compute their sum as a string.

    • Compare with the next substring in s.

  4. If we reach the end exactly, return true; otherwise backtrack.

βš™οΈ Algorithm Steps:

  1. Let n = s.length().

  2. For len1 from 1 to n-2, and for len2 from 1 to n - len1 - 1:

    • If s[0..len1-1] or s[len1..len1+len2-1] invalid (leading zero), continue.

    • Call check(0, len1, len2). If it returns true, answer true.

  3. If no split works, return false.

Helper bool check(int idx, int l1, int l2):

  • Extract x = s.substr(idx, l1), y = s.substr(idx+l1, l2).

  • Compute z = addStrings(x,y); let l3 = z.length().

  • If substring at idx + l1 + l2 of length l3 β‰  z, return false.

  • If exactly reached end, return true.

  • Else recurse with (idx + l1, l2, l3).

πŸ“ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(nΒ³)

    • Triple nested loops over (len1, len2) and recursive checks with string-addition/substr of O(n) each.

  • Expected Auxiliary Space Complexity: O(n)

    • Recursion depth and temporary strings up to length n.

πŸ§‘β€πŸ’» Code (C++)

⚑ View Alternative Approaches with Code and Analysis

πŸ“Š 2️⃣ Brute Force All Splits + Manual Addition

πŸ’‘ Algorithm Steps:

  1. Try all possible pairs of substrings for the first two numbers (a, b) in the string.

  2. For each pair:

    • Skip if either a or b starts with '0' and is longer than 1.

  3. Use a helper add() function to simulate string-based addition.

  4. Use DFS to check if the rest of the string follows the sum pattern: a + b = c, b + c = d, etc.

πŸ“ Complexity Analysis:

  • Time: ⏱️ O(NΒ³) – due to triple nested traversal & substring checks

  • Auxiliary Space: πŸ’Ύ O(N) – for string recursion stack

βœ… Why This Approach?

  • Handles string overflow issues using manual addition.

  • Good educational value to learn string manipulation + recursion.

πŸ†š πŸ” Comparison of Approaches

πŸš€ Approach

⏱️ Time Complexity

πŸ’Ύ Space Complexity

βœ… Pros

⚠️ Cons

πŸ” Recursion + String Addition

🟒 O(N³)

🟒 O(N)

Accurate, string-safe, recursive

Slower due to overhead and deep calls

πŸ”Ž Brute Force + Manual Add

πŸ”Έ O(NΒ³)

πŸ”Έ O(N)

Simpler logic, useful for learning

Redundant with recursion, less optimal

πŸ† Best Choice Recommendation

🎯 Scenario

πŸŽ–οΈ Recommended Approach

πŸ“ Long strings or potential overflow issues

πŸ₯‡ String Add + Recursion

πŸ“˜ Educational brute-force understanding

πŸ₯ˆ Manual Brute Force (DFS)

πŸ§‘β€πŸ’» Code (Java)

🐍 Code (Python)

🧠 Contribution and Support

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: πŸ“¬ Any Questions?. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Visitor counter

Last updated