#http://p...content-available-to-author-only...e.com/q/268022/51441
# s:
# input digit string that should be checked
# L:
# the start of the substring that should be investigated
# L:
# the end of the substring that should be investigated
# cnt:
# number of 3divisble substrings found so far
# ss0:
# number of substrings that end at the current position
# and are divisible by 3
# ss1:
# number of substrings that end at the current position
# and have the remainder 1 when divided by 3
# ss2:
# number of substrings that end at the current position
# and have the remainder 2 when divided by 3
def ss(s,L,R):
cnt=0
(ss0,ss1,ss2)=(0,0,0)
for i in range(L,R+1):
r=int(s[i])%3
if r==0:
ss0+=1
elif r==1:
(ss0,ss1,ss2)=(ss2,ss0,ss1)
ss1+=1
elif r==2:
(ss0,ss1,ss2)=(ss1,ss2,ss0)
ss2+=1
cnt+=ss0
return(cnt)
print(ss('392301',0,len('392301')-1))
print(ss('2035498',2,5))