Check if a permutation of S2 can be obtained by adding or removing characters from S1 | Function to check if the given number is prime or not ; If the number is less than 2 ; If the number is 2 ; If N is a multiple of 2 ; Otherwise , check for the odds values ; Function to check if S1 can be a permutation of S2 by adding or removing characters from S1 ; Initialize a frequency array ; Decrement the frequency for occurrence in s1 ; Increment the frequency for occurence in s2 ; If frequency of current char is same in s1 and s2 ; Check the frequency for the current char is not prime ; Print the result ; Driver Code
function isPrime ( n ) { if ( n <= 1 ) return false ; else if ( n == 2 ) return true ; else if ( n % 2 == 0 ) return false ; for ( let i = 3 ; i <= Math . floor ( Math . sqrt ( n ) ) ; i += 2 ) { if ( n % i == 0 ) return false ; } return true ; } function checkPermutation ( s1 , s2 ) { let freq = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) freq [ i ] = 0 ; for ( let ch = 0 ; ch < s1 . length ; ch ++ ) { freq [ s1 [ ch ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; } for ( let ch = 0 ; ch < s2 . length ; ch ++ ) { freq [ s2 [ ch ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let isAllChangesPrime = true ; for ( let i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) { continue ; } else if ( ! isPrime ( Math . abs ( freq [ i ] ) ) ) { isAllChangesPrime = false ; break ; } } if ( isAllChangesPrime ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let S1 = " " ; let S2 = " " ; checkPermutation ( S1 , S2 ) ;