A value-equal string is a string where all characters are the same.
"1111" and "33" are value-equal strings."123" is not a value-equal string.Given a digit string s, decompose the string into some number of consecutive value-equal substrings where exactly one substring has a length of 2 and the remaining substrings have a length of 3.
Return true if you can decompose s according to the above rules. Otherwise, return false.
A substring is a contiguous sequence of characters in a string.
Example 1:
Input: s = "000111000" Output: false Explanation: s cannot be decomposed according to the rules because ["000", "111", "000"] does not have a substring of length 2.
Example 2:
Input: s = "00011111222" Output: true Explanation: s can be decomposed into ["000", "111", "11", "222"].
Example 3:
Input: s = "011100022233" Output: false Explanation: s cannot be decomposed according to the rules because of the first '0'.
Constraints:
1 <= s.length <= 1000s consists of only digits '0' through '9'.We traverse the string s, using two pointers i and j to count the length of each equal substring. If the length modulo 3 is 1, it means that the length of this substring does not meet the requirements, so we return false. If the length modulo 3 is 2, it means that a substring of length 2 has appeared. If a substring of length 2 has appeared before, return false, otherwise assign the value of j to i and continue to traverse.
After the traversal, check whether a substring of length 2 has appeared. If not, return false, otherwise return true.
The time complexity is O(n), where n is the length of the string s. The space complexity is O(1).
Java
C++
Go
TypeScript
Palindromic Substrings - Leetcode 647 - Python • NeetCode • 177,962 views views
Watch 9 more video solutions →Practice Check if String Is Decomposable Into Value-Equal Substrings with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor