Check if String Is Decomposable Into Value-Equal Substrings

🏠 ⬅️ ➑️

A value-equal string is a string where all characters are the same.

  • For example, "1111 " and "33 " are value-equal strings.
  • In contrast, "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 <= 1000
  • s consists of only digits '0' through '9'.

Note: This problem is from LeetCode.
❌ Compiled
❌ Executed
❌ Correct
program main
    implicit none

    character(len=100) :: s
    logical :: result

    ! Example 1
    s = "000111000 "
    write(*,*) s, "is", (result == .true.), "a value-equal string"

    ! Example 2
    s = "00011111222 "
    write(*,*) s, "is", (result == .true.), "a value-equal string"

    ! Example 3
    s = "011100022233 "
    write(*,*) s, "is", (result == .true.), "a value-equal string"

contains

    function value_equal(str) result(res)
        implicit none
        character(len=*), intent(in) :: str
        integer :: i, j
        logical :: res

        res = .true.
        do i = 1, len(str)-1
            if (str(i:i+1) /= str(1:2)) then
                res = .false.
                exit
            end if
        end do
    end function value_equal

end program main
❌ Compiled
❌ Executed
❌ Correct
! Declare variables
integer :: i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
character(len=1000) :: s
logical :: flag

! Initialize variables
i = 0
j = 0
k = 0
l = 0
m = 0
n = 0
o = 0
p = 0
q = 0
r = 0
s = 0
t = 0
u = 0
v = 0
w = 0
x = 0
y = 0
z = 0
flag = .false.

! Read input string
read(*,*) s

! Check if input string is value-equal
do i = 1, len(s) - 1
    if (s(i:i+1) /= s(i+1:i+2)) then
        flag = .false.
        exit
    end if
end do

! Check if input string has a length of 2
if (len(s) == 2) then
    flag = .true.
else
    ! Check if input string has a length of 3
    if (len(s) == 3) then
        ! Check if input string is value-equal
        if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3)) then
            flag = .true.
        end if
    else
        ! Check if input string has a length of 4
        if (len(s) == 4) then
            ! Check if input string is value-equal
            if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3) .and. s(3:3) == s(4:4)) then
                flag = .true.
            end if
        else
            ! Check if input string has a length of 5
            if (len(s) == 5) then
                ! Check if input string is value-equal
                if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3) .and. s(3:3) == s(4:4) .and. s(4:4) == s(5:5)) then
                    flag = .true.
                end if
            else
                ! Check if input string has a length of 6
                if (len(s) == 6) then
                    ! Check if input string is value-equal
                    if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3) .and. s(3:3) == s(4:4) .and. s(4:4) == s(5:5) .and. s(5:5) == s(6:6)) then
                        flag = .true.
                    end if
                else
                    ! Check if input string has a length of 7
                    if (len(s) == 7) then
                        ! Check if input string is value-equal
                        if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3) .and. s(3:3) == s(4:4) .and. s(4:4) == s(5:5) .and. s(5:5) == s(6:6) .and. s(6:6) == s(7:7)) then
                            flag = .true.
                        end if
                    else
                        ! Check if input string has a length of 8
                        if (len(s) == 8) then
                            ! Check if input string is value-equal
                            if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3) .and. s(3:3) == s(4:4) .and. s(4:4) == s(5:5) .and. s(5:5) == s(6:6) .and. s(6:6) == s(7:7) .and. s(7:7) == s(8:
🌐 Data from online sources
def num_different_integers(word):
    for c in word:
        if not c.isdigit():
            word = word.replace(c, ' ')
    nums = word.split()
    unique_nums = set()
    for num in nums:
        num = num.lstrip('0') or '0'
        unique_nums.add(num)
    return len(unique_nums)
  1. Replace all non-digit characters with a space in word.
  2. Split the modified word by spaces to get the list of integers.
  3. Initialize an empty Set named uniqueNums to store unique integers.
  4. For each integer in the list, remove leading zeros (if any) and add it to the uniqueNums set.
  5. Return the size of uniqueNums which is the count of unique integers.
🌐 Data from online sources
#include <string>
#include <unordered_set>

int numDifferentIntegers(std::string word) {
    for (char &c : word) {
        if (c < '0' || c > '9') {
            c = ' ';
        }
    }
    std::istringstream iss(word);
    std::string num;
    std::unordered_set<std::string> uniqueNums;
    while (iss >> num) {
        num.erase(0, num.find_first_not_of('0'));
        uniqueNums.insert(num);
    }
    return uniqueNums.size();
}
  1. Replace all non-digit characters with a space in word.
  2. Split the modified word by spaces to get the list of integers.
  3. Initialize an empty Set named uniqueNums to store unique integers.
  4. For each integer in the list, remove leading zeros (if any) and add it to the uniqueNums set.
  5. Return the size of uniqueNums which is the count of unique integers.