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 <= 1000
s
consists of only digits '0'
through '9'
.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
temp.f95:9:25: 9 | write(*,*) s, "is", (result == .true.), "a value-equal string" | 1 Error: Logicals at (1) must be compared with .eqv. instead of == temp.f95:13:25: 13 | write(*,*) s, "is", (result == .true.), "a value-equal string" | 1 Error: Logicals at (1) must be compared with .eqv. instead of == temp.f95:17:25: 17 | write(*,*) s, "is", (result == .true.), "a value-equal string" | 1 Error: Logicals at (1) must be compared with .eqv. instead of ==
! 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:
temp.f95:3:24: 3 | character(len=1000) :: s | 1 Error: Symbol βsβ at (1) already has basic type of INTEGER temp.f95:32:10: 32 | if (s(i:i+1) /= s(i+1:i+2)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:35:7: 35 | end if | 1 Error: Expecting END DO statement at (1) temp.f95:45:14: 45 | if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:41:4: 41 | else | 1 ...... 48 | else | 2 Error: Duplicate ELSE statements at (1) and (2) temp.f95:52:18: 52 | if (s(1:1) == s(2:2) .and. s(2:2) == s(3:3) .and. s(3:3) == s(4:4)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:41:4: 41 | else | 1 ...... 55 | else | 2 Error: Duplicate ELSE statements at (1) and (2) temp.f95:59:22: 59 | 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 | 1 Error: Syntax error in IF-expression at (1) temp.f95:41:4: 41 | else | 1 ...... 62 | else | 2 Error: Duplicate ELSE statements at (1) and (2) temp.f95:66:132: 66 | 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 | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:66:132: 66 | 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 | 1 Error: Missing β)β in statement at or before (1) temp.f95:41:4: 41 | else | 1 ...... 69 | else | 2 Error: Duplicate ELSE statements at (1) and (2) temp.f95:73:132: 73 | 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 | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:73:132: 73 | 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 | 1 Error: Missing β)β in statement at or before (1) temp.f95:41:4: 41 | else | 1 ...... 76 | else | 2 Error: Duplicate ELSE statements at (1) and (2) temp.f95:80:132: 80 | 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: | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:80:132: 80 | 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: | 1 Error: Missing β)β in statement at or before (1) f951: Error: Unexpected end of file in βtemp.f95β f951: some warnings being treated as errors
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)
word
.word
by spaces to get the list of integers.uniqueNums
to store unique integers.uniqueNums
set.uniqueNums
which is the count of unique integers.#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();
}
word
.word
by spaces to get the list of integers.uniqueNums
to store unique integers.uniqueNums
set.uniqueNums
which is the count of unique integers.