Find total no of collisions taking place between the balls in which initial direction of each ball is given | Java implementation to Find total no of collisions taking place between the balls in which initial direction of each ball is given ; Function to count no of collision ; length of the string ; Driver code
import java . io . * ; class GFG { static int count ( String s ) { int N , i , cnt = 0 , ans = 0 ; N = s . length ( ) ; for ( i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' R ' ) cnt ++ ; if ( s . charAt ( i ) == ' L ' ) ans += cnt ; } return ans ; } static public void main ( String [ ] args ) { String s = " RRLL " ; System . out . println ( count ( s ) ) ; } }