Check if the given string is K | Function that return true if sub - string of length k starting at index i is also a prefix of the string ; k length sub - string cannot start at index i ; Character mismatch between the prefix and the sub - string starting at index i ; Function that returns true if str is K - periodic ; Check whether all the sub - strings str [ 0 , k - 1 ] , str [ k , 2 k - 1 ] ... are equal to the k length prefix of the string ; Driver code
function isPrefix ( str , len , i , k ) { if ( i + k > len ) return false ; for ( let j = 0 ; j < k ; j ++ ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; } return true ; } function isKPeriodic ( str , len , k ) { for ( let i = k ; i < len ; i += k ) if ( ! isPrefix ( str , len , i , k ) ) return false ; return true ; } let str = " " ; let len = str . length ; let k = 5 ; if ( isKPeriodic ( str , len , k ) ) document . write ( " " ) ; else document . write ( " " ) ;