Count of character pairs at same distance as in English alphabets | Method to count pairs ; Increment count if characters are at same distance ; Driver code
function countPairs ( str ) { let result = 0 ; let n = str . length ; for ( let i = 0 ; i < n ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( str [ i ] . charCodeAt ( ) - str [ j ] . charCodeAt ( ) ) == Math . abs ( i - j ) ) result ++ ; return result ; } let str = " " ; document . write ( countPairs ( str ) ) ;