Skip to content

Commit

Permalink
New Problem Solution - "1849. Splitting a String Into Descending Cons…
Browse files Browse the repository at this point in the history
…ecutive Values"
  • Loading branch information
haoel committed May 3, 2021
1 parent bf4fe68 commit 83fa31a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1849|[Splitting a String Into Descending Consecutive Values](https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/) | [C++](./algorithms/cpp/splittingAStringIntoDescendingConsecutiveValues/SplittingAStringIntoDescendingConsecutiveValues.cpp)|Medium|
|1848|[Minimum Distance to the Target Element](https://leetcode.com/problems/minimum-distance-to-the-target-element/) | [C++](./algorithms/cpp/minimumDistanceToTheTargetElement/MinimumDistanceToTheTargetElement.cpp)|Easy|
|1847|[Closest Room](https://leetcode.com/problems/closest-room/) | [C++](./algorithms/cpp/closestRoom/ClosestRoom.cpp)|Hard|
|1846|[Maximum Element After Decreasing and Rearranging](https://leetcode.com/problems/maximum-element-after-decreasing-and-rearranging/) | [C++](./algorithms/cpp/maximumElementAfterDecreasingAndRearranging/MaximumElementAfterDecreasingAndRearranging.cpp)|Medium|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Source : https://leetcode.com/problems/splitting-a-string-into-descending-consecutive-values/
// Author : Hao Chen
// Date : 2021-05-03

/*****************************************************************************************************
*
* You are given a string s that consists of only digits.
*
* Check if we can split s into two or more non-empty substrings such that the numerical values of the
* substrings are in descending order and the difference between numerical values of every two
* adjacent substrings is equal to 1.
*
* For example, the string s = "0090089" can be split into ["0090", "089"] with numerical
* values [90,89]. The values are in descending order and adjacent values differ by 1, so this way is
* valid.
* Another example, the string s = "001" can be split into ["0", "01"], ["00", "1"], or ["0",
* "0", "1"]. However all the ways are invalid because they have numerical values [0,1], [0,1], and
* [0,0,1] respectively, all of which are not in descending order.
*
* Return true if it is possible to split s​​​​​​ as described above, or false otherwise.
*
* A substring is a contiguous sequence of characters in a string.
*
* Example 1:
*
* Input: s = "1234"
* Output: false
* Explanation: There is no valid way to split s.
*
* Example 2:
*
* Input: s = "050043"
* Output: true
* Explanation: s can be split into ["05", "004", "3"] with numerical values [5,4,3].
* The values are in descending order with adjacent values differing by 1.
*
* Example 3:
*
* Input: s = "9080701"
* Output: false
* Explanation: There is no valid way to split s.
*
* Example 4:
*
* Input: s = "10009998"
* Output: true
* Explanation: s can be split into ["100", "099", "98"] with numerical values [100,99,98].
* The values are in descending order with adjacent values differing by 1.
*
* Constraints:
*
* 1 <= s.length <= 20
* s only consists of digits.
******************************************************************************************************/

class Solution {
private:
int pos;
public:
bool getNum(string& s, long target) {

long n = 0;
while(s[pos] == '0') pos++;
while(pos < s.size()){
n = (n*10 + s[pos++] - '0') ;
if (n == target) return true;
if (n > target ) return false; // n is already greater than target
}
return target == n;
}

long firstNum(string& s, int len) {
long n = 0;
while(s[pos] == '0') pos++;
for(; pos< s.size() && len > 0; pos++, len--) {
n = (n*10 + s[pos] - '0') ;
}
return n;
}

bool splitString(string s) {
pos = 0;
long num;
for (int len=1; len<= s.size()/2+1; len++) {
pos = 0;
num = firstNum(s, len);
if (pos >= s.size()) continue; // only have one number

bool result = true;
while( pos < s.size() ) {
if (getNum(s, --num) == false){
result = false;
break;
}
}
if (result) return true;
}
return false;
}
};

0 comments on commit 83fa31a

Please sign in to comment.