Count of words whose i | Return the count of words . ; If word contain single letter , return 1. ; Checking for first letter . ; Traversing the string and multiplying for combinations . ; If all three letters are same . ; If two letter are distinct . ; If all three letter are distinct . ; Checking for last letter . ; Driver Code
< ? php function countWords ( $ str , $ len ) { $ count = 1 ; if ( $ len == 1 ) return $ count ; if ( $ str [ 0 ] == $ str [ 1 ] ) $ count *= 1 ; else $ count *= 2 ; for ( $ j = 1 ; $ j < $ len - 1 ; $ j ++ ) { if ( $ str [ $ j ] == $ str [ $ j - 1 ] && $ str [ $ j ] == $ str [ $ j + 1 ] ) $ count *= 1 ; else if ( $ str [ $ j ] == $ str [ $ j - 1 ] $ str [ $ j ] == $ str [ $ j + 1 ] $ str [ $ j - 1 ] == $ str [ $ j + 1 ] ) $ count *= 2 ; else $ count *= 3 ; } if ( $ str [ $ len - 1 ] == $ str [ $ len - 2 ] ) $ count *= 1 ; else $ count *= 2 ; return $ count ; } $ str = " abc " ; $ len = strlen ( $ str ) ; echo countWords ( $ str , $ len ) ; ? >