LeetCode 2522: Partition String Into Substrings With Values at Most K Solution

Master LeetCode problem 2522 (Partition String Into Substrings With Values at Most K), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2522. Partition String Into Substrings With Values at Most K

Problem Explanation

Explanation:

To solve this problem, we can iterate through the given string s and maintain a running sum of the current substring value. If the value of the current substring exceeds k, we increment the count of substrings and reset the running sum.

Algorithm:

  1. Initialize variables count to store the number of substrings, curValue to store the current substring value, and i as the index.
  2. Iterate over the characters in the string s.
  3. For each character:
    • Calculate the integer value of the character.
    • Update curValue by multiplying it by 10 and adding the integer value.
    • If curValue exceeds k, increment count and reset curValue to the integer value of the current character.
  4. Finally, check if curValue is greater than 0. If so, increment count.
  5. If there are no good partitions (i.e., count is 0), return -1. Otherwise, return count.

Time Complexity:

The time complexity of this algorithm is O(n), where n is the length of the input string s.

Space Complexity:

The space complexity is O(1) as we are using a constant amount of extra space.

:

Solution Code

class Solution {
    public int partitionString(String s, int k) {
        int count = 0;
        int curValue = 0;
        
        for (int i = 0; i < s.length(); i++) {
            int digitValue = s.charAt(i) - '0';
            curValue = curValue * 10 + digitValue;
            
            if (curValue > k) {
                count++;
                curValue = digitValue;
            }
        }
        
        if (curValue > 0) {
            count++;
        }
        
        return count == 0 ? -1 : count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2522 (Partition String Into Substrings With Values at Most K)?

This page provides optimized solutions for LeetCode problem 2522 (Partition String Into Substrings With Values at Most K) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2522 (Partition String Into Substrings With Values at Most K)?

The time complexity for LeetCode 2522 (Partition String Into Substrings With Values at Most K) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2522 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2522 in Java, C++, or Python.

Back to LeetCode Solutions