This page is still under construction.

Parts of this page are still being built. What you see may change.

JJOOII

Interview

Time limit1sMemory limit128 MB

Summary
Find the largest k such that k J's, then k O's, then k I's appear consecutively in the given string.
Level

Medium5 of 10

Topics
String, Prefix sum, Binary search
Solved
No attempts yet

Problem

We work with strings made up of only the three characters J, O, and I.

A string tt is a substring of a string ss if ss can be formed by adding zero or more characters to both the front and the back of tt; that is, tt must appear contiguously inside ss. For example, JJOOII is a substring of OJJOOIIOJOI, whereas JOI is not a substring of JOOI.

For a non-negative integer kk, a level-kk JOI sequence is the string consisting of kk copies of J, then kk copies of O, then kk copies of I, in that order. For example, JJOOII is a level-22 JOI sequence.

You are given a string SS of length NN over the characters J, O, and I. Find the largest kk such that a level-kk JOI sequence is a substring of SS.

Input

The first line contains the string SS, made up of the characters J, O, and I.

Output

Print, on a single line, the largest kk such that a level-kk JOI sequence is a substring of SS. (If no level-11-or-higher JOI sequence is a substring, print 0.)

Constraints

  • 1≤N≤1 000 0001 \le N \le 1\,000\,000, where NN is the length of SS.

Examples4

  1. Example 1

    Input
    OJJOOIIOJOI
    
    Expected output
    2
    
  2. Example 2

    Input
    IJJIIJJJ
    
    Expected output
    0
    
  3. Example 3

    Input
    JOIJOIJOIJOIJOI
    
    Expected output
    1
    
  4. Example 4

    Input
    OOJJJJJJJOOOOIIIII
    
    Expected output
    4