codeparrot/xlcost-text-to-code huggingface dataset:
Remove duplicates from a string in O ( 1 ) extra space | Method to remove duplicates ; table to keep track of visited characters ; to keep track of end index of resultant string ; Driver code
function removeDuplicatesFromString ( string ) { let table = new Array ( 256 ) ; for ( let i = 0 ; i < table . length ; i ++ ) table [ i ] = 0 ; let chars = string . split ( " " ) ; let endIndex = 0 ; for ( let i = 0 ; i < chars . length ; i ++ ) { if ( table [ chars [ i ] . charCodeAt ( 0 ) ] == 0 ) { table [ chars [ i ] . charCodeAt ( 0 ) ] = - 1 ; chars [ endIndex ++ ] = chars [ i ] ; } } let ans = " " ; for ( let i = 0 ; i < endIndex ; i ++ ) ans += chars [ i ] return ans ; } let str = " " ; document . write ( removeDuplicatesFromString ( str ) ) ;
Check if the characters in a string form a Palindrome in O ( 1 ) extra space | Utility function to get the position of first character in the string ; Get the position of first character in the string ; Utility function to get the position of last character in the string ; Get the position of last character in the string ; Function to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; break , when all letters are checked ; if mismatch found , break the loop ; Driver code
function firstPos ( str , start , end ) { let firstChar = - 1 ; for ( let i = start ; i <= end ; i ++ ) { if ( str [ i ] >= ' ' && str [ i ] <= ' ' ) { firstChar = i ; break ; } } return firstChar ; } function lastPos ( str , start , end ) { let lastChar = - 1 ; for ( let i = start ; i >= end ; i -- ) { if ( str [ i ] >= ' ' && str [ i ] <= ' ' ) { lastChar = i ; break ; } } return lastChar ; } function isPalindrome ( str ) { let firstChar = 0 , lastChar = str . length - 1 ; let ch = true ; for ( let i = 0 ; i < str . length ; i ++ ) { firstChar = firstPos ( str , firstChar , lastChar ) ; lastChar = lastPos ( str , lastChar , firstChar ) ; if ( lastChar < 0 firstChar < 0 ) break ; if ( str [ firstChar ] == str [ lastChar ] ) { firstChar ++ ; lastChar -- ; continue ; } ch = false ; break ; } return ch ; } let str = " " ; if ( isPalindrome ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum length subsequence possible of the form R ^ N K ^ N | function to calculate the maximum length of substring of the form R ^ nK ^ n ; Count no . Of R 's before a K ; Count no . Of K 's after a K ; Update maximum length ; Driver code
function find ( s ) { let max = 0 , i , j = 0 , countk = 0 , countr = 0 ; let table = new Array ( s . length ) ; for ( let i = 0 ; i < s . length ; i ++ ) { table [ i ] = new Array ( 2 ) ; } for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) countr ++ ; else table [ j ++ ] [ 0 ] = countr ; } j -- ; for ( i = s . length - 1 ; i >= 0 ; i -- ) { if ( s [ i ] == ' ' ) { countk ++ ; table [ j -- ] [ 1 ] = countk ; } if ( Math . min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) > max ) max = Math . min ( table [ j + 1 ] [ 0 ] , table [ j + 1 ] [ 1 ] ) ; } return max ; } let s = " " ; let n = find ( s ) ; document . write ( n ) ;
Longest common anagram subsequence from N strings | JavaScript program to find longest possible subsequence anagram of N strings . ; function to store frequency of each character in each string ; function to Find longest possible sequence of N strings which is anagram to each other ; to get lexicographical largest sequence . ; find minimum of that character ; print that character minimum number of times ; Driver code ; to store frequency of each character in each string ; to get frequency of each character ; function call
let MAX_CHAR = 26 ; function frequency ( fre , s , n ) { for ( let i = 0 ; i < n ; i ++ ) { let str = s [ i ] ; for ( let j = 0 ; j < str . length ; j ++ ) fre [ i ] [ str [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } } function LongestSequence ( fre , n ) { for ( let i = 24 ; i >= 0 ; i -- ) { let mi = fre [ 0 ] [ i ] ; for ( let j = 1 ; j < n ; j ++ ) mi = Math . min ( fre [ j ] [ i ] , mi ) ; while ( mi -- != 0 ) document . write ( String . fromCharCode ( ' ' . charCodeAt ( 0 ) + i ) ) ; } } let s = [ " " , " " , " " ] ; let n = s . length ; let fre = new Array ( n ) ; for ( let i = 0 ; i < n ; i ++ ) { fre [ i ] = new Array ( 26 ) ; for ( let j = 0 ; j < 26 ; j ++ ) fre [ i ] [ j ] = 0 ; } frequency ( fre , s , n ) ; LongestSequence ( fre , n ) ;
Check if two strings are permutation of each other | Function to check whether two strings are Permutation of each other ; Get lengths of both strings ; If length of both strings is not same , then they cannot be Permutation ; Sort both strings ; Compare sorted strings ; Driver Code
function arePermutation ( str1 , str2 ) { let n1 = str1 . length ; let n2 = str2 . length ; if ( n1 != n2 ) return false ; let ch1 = str1 . split ( ' ' ) ; let ch2 = str2 . split ( ' ' ) ; ch1 . sort ( ) ; ch2 . sort ( ) ; for ( let i = 0 ; i < n1 ; i ++ ) if ( ch1 [ i ] != ch2 [ i ] ) return false ; return true ; } let str1 = " " ; let str2 = " " ; if ( arePermutation ( str1 , str2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if two strings are permutation of each other | Javascript function to check whether two strings are Permutations of each other ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array
function arePermutation ( str1 , str2 ) { let count = new Array ( NO_OF_CHARS ) ; let i ; for ( i = 0 ; str1 [ i ] && str2 [ i ] ; i ++ ) { count [ str1 [ i ] ] ++ ; count [ str2 [ i ] ] -- ; } if ( str1 [ i ] str2 [ i ] ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; }
Maximum power of jump required to reach the end of string | Function to calculate the maximum power of the jump ; Initialize the count with 1 ; Find the letacter at last index ; Start traversing the string ; Check if the current let is equal to the last letacter ; max_so_far stores maximum value of the power of the jump from starting to ith position ; Reset the count to 1 ; Else , increment the number of jumps / count ; Return the maximum number of jumps ; Driver code
function powerOfJump ( s ) { let count = 1 ; let max_so_far = Number . MIN_VALUE ; let ch = s [ s . length - 1 ] ; for ( let i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ch ) { if ( count > max_so_far ) { max_so_far = count ; } count = 1 ; } else count ++ ; } return max_so_far ; } let st = " " ; document . write ( powerOfJump ( st ) ) ;
Longest substring with count of 1 s more than 0 s | Function to find longest substring having count of 1 s more than count of 0 s . ; To store sum . ; To store first occurrence of each sum value . ; To store maximum length . ; To store current substring length . ; Add 1 if current character is 1 else subtract 1. ; If sum is positive , then maximum length substring is bin [ 0. . i ] ; If sum is negative , then maximum length substring is bin [ j + 1. . i ] , where sum of substring bin [ 0. . j ] is sum - 1. ; Make entry for this sum value in hash table if this value is not present . ; Driver code
function findLongestSub ( bin ) { let n = bin . length , i ; let sum = 0 ; let prevSum = new Map ( ) ; let maxlen = 0 ; let currlen ; for ( i = 0 ; i < n ; i ++ ) { if ( bin [ i ] == ' ' ) sum ++ ; else sum -- ; if ( sum > 0 ) { maxlen = i + 1 ; } else if ( sum <= 0 ) { if ( prevSum . has ( sum - 1 ) ) { currlen = i - ( prevSum . get ( sum - 1 ) == null ? 1 : prevSum . get ( sum - 1 ) ) ; maxlen = Math . max ( maxlen , currlen ) ; } } if ( ! prevSum . has ( sum ) ) prevSum . set ( sum , i ) ; } return maxlen ; } let bin = " " ; document . write ( findLongestSub ( bin ) ) ;
Average of ASCII values of characters of a given string | Function to find average of ASCII value of chars ; loop to sum the ascii value of chars ; Returning average of chars ; Driver code
function averageValue ( s ) { let sum_char = 0 ; for ( let i = 0 ; i < s . length ; i ++ ) { sum_char += ( s [ i ] ) . charCodeAt ( 0 ) ; } return Math . floor ( sum_char / s . length ) ; } let s = " " ; document . write ( averageValue ( s ) ) ;
Check if two same sub | Function to check if similar subsequences occur in a string or not ; iterate and count the frequency ; counting frequency of the letters ; check if frequency is more than once of any character ; Driver Code
function check ( s , l ) { let freq = new Array ( 26 ) . fill ( 0 ) ; for ( let i = 0 ; i < l ; i ++ ) { freq [ s [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; } for ( let i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] >= 2 ) return true ; } return false ; } let s = " " ; let l = s . length ; if ( check ( s , l ) ) document . write ( " " ) ; else document . write ( " " ) ;
Number of Counterclockwise shifts to make a string palindrome | Function to check if given string is palindrome or not . ; Function to find counter clockwise shifts to make string palindrome . ; Pointer to starting of current shifted string . ; Pointer to ending of current shifted string . ; Concatenate string with itself ; To store counterclockwise shifts ; Move left and right pointers one step at a time . ; Check if current shifted string is palindrome or not ; If string is not palindrome then increase count of number of shifts by 1. ; Driver code .
function isPalindrome ( str , l , r ) { while ( l < r ) { if ( str [ l ] != str [ r ] ) return false ; l ++ ; r -- ; } return true ; } function CyclicShifts ( str ) { var n = str . length ; var left = 0 ; var right = n - 1 ; str = str + str ; var cnt = 0 ; while ( right < 2 * n - 1 ) { if ( isPalindrome ( str , left , right ) ) break ; cnt ++ ; left ++ ; right ++ ; } return cnt ; } var str = " " ; document . write ( CyclicShifts ( str ) ) ;
Find a string such that every character is lexicographically greater than its immediate next character | Function that prints the required string ; Find modulus with 26 ; Print extra characters required ; Print the given reverse string countOfStr times ; Driver Code ; Initialize a string in reverse order
function printString ( n , str ) { var str2 = " " ; var extraChar = n % 26 ; if ( extraChar >= 1 ) { for ( var i = 26 - ( extraChar + 1 ) ; i <= 25 ; i ++ ) str2 += str [ i ] ; } var countOfStr = parseInt ( n / 26 ) ; for ( var i = 1 ; i <= countOfStr ; i ++ ) { for ( var j = 0 ; j < 26 ; j ++ ) str2 += str [ j ] ; } return str2 ; } var n = 30 ; var str = " " ; document . write ( printString ( n , str ) ) ;
Longest Common Prefix Matching | Set | A Utility Function to find the common prefix between first and last strings ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; sorts the N set of strings ; prints the common prefix of the first and the last String of the set of strings ; Driver Code
function commonPrefixUtil ( str1 , str2 ) { let result = " " ; let n1 = str1 . length , n2 = str2 . length ; for ( let i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) { break ; } result += str1 [ i ] ; } return ( result ) ; } function commonPrefix ( arr , n ) { arr . sort ( ) ; document . write ( commonPrefixUtil ( arr [ 0 ] , arr [ n - 1 ] ) + " " ) ; } let arr = [ " " , " " , " " , " " ] ; let n = arr . length ; commonPrefix ( arr , n ) ;
Square of large number represented as String | Multiplies str1 and str2 , and prints result . ; will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multiplication of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result at current position . ; Carry for next iteration ; Store result ; store carry in next cell ; To shift position to left after every multiplication of a digit in num1 . ; ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0' ; generate the result string ; Driver code
function multiply ( num1 , num2 ) { let n1 = num1 . length ; let n2 = num2 . length ; if ( n1 == 0 n2 == 0 ) return " " ; let result = new Array ( n1 + n2 ) ; for ( let i = 0 ; i < result . length ; i ++ ) { result [ i ] = 0 ; } let i_n1 = 0 ; let i_n2 = 0 ; for ( let i = n1 - 1 ; i >= 0 ; i -- ) { let carry = 0 ; let n_1 = num1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; i_n2 = 0 ; for ( let j = n2 - 1 ; j >= 0 ; j -- ) { let n_2 = num2 [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; let sum = n_1 * n_2 + result [ i_n1 + i_n2 ] + carry ; carry = Math . floor ( sum / 10 ) ; result [ i_n1 + i_n2 ] = sum % 10 ; i_n2 ++ ; } if ( carry > 0 ) result [ i_n1 + i_n2 ] += carry ; i_n1 ++ ; } let i = result . length - 1 ; while ( i >= 0 && result [ i ] == 0 ) i -- ; if ( i == - 1 ) return " " ; let s = " " ; while ( i >= 0 ) s += ( result [ i -- ] ) . toString ( ) ; return s ; } let str1 = " " ; document . write ( multiply ( str1 , str1 ) ) ;
Perform n steps to convert every digit of a number in the format [ count ] [ digit ] | Function to perform every step ; Perform N steps ; Traverse in the string ; For last digit ; Recur for current string ; Driver code
function countDigits ( st , n ) { if ( n > 0 ) { let cnt = 1 , i ; let st2 = " " ; for ( i = 1 ; i < st . length ; i ++ ) { if ( st [ i ] == st [ i - 1 ] ) cnt ++ ; else { st2 += String . fromCharCode ( ' ' . charCodeAt ( ) + cnt ) ; st2 += st [ i - 1 ] ; cnt = 1 ; } } st2 += String . fromCharCode ( ' ' . charCodeAt ( ) + cnt ) ; st2 += st [ i - 1 ] ; countDigits ( st2 , -- n ) ; } else document . write ( st ) ; } let num = " " ; let n = 3 ; countDigits ( num , n ) ;
Check if the given string of words can be formed from words present in the dictionary | Function to check if the word is in the dictionary or not ; map to store all words in dictionary with their count ; Adding all words in map ; Search in map for all words in the sentence ; All words of sentence are present ; Driver code ; Calling function to check if words are present in the dictionary or not
function match_words ( dictionary , sentence , n , m ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( mp . has ( dictionary [ i ] ) ) { mp . set ( dictionary [ i ] , mp . get ( dictionary [ i ] ) + 1 ) ; } else { mp . set ( dictionary [ i ] , 1 ) ; } } for ( let i = 0 ; i < m ; i ++ ) { if ( mp . has ( sentence [ i ] ) ) mp . set ( sentence [ i ] , mp . get ( sentence [ i ] ) - 1 ) ; else return false ; } return true ; } let dictionary = [ " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; let n = dictionary . length ; let sentence = [ " " , " " , " " , " " , " " , " " , " " ] ; let m = sentence . length ; if ( match_words ( dictionary , sentence , n , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Print all 3 digit repeating numbers in a very large number | function to print 3 digit repeating numbers ; Hashmap to store the frequency of a 3 digit number ; first three digit number ; if key already exists increase value by 1 ; Output the three digit numbers with frequency > 1 ; Input string ; Calling Function
function printNum ( s ) { let i = 0 , j = 0 , val = 0 ; let hm = new Map ( ) ; val = ( s [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) * 100 + ( s [ 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) * 10 + ( s [ 2 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; hm . set ( val , 1 ) ; for ( i = 3 ; i < s . length ; i ++ ) { val = ( val % 100 ) * 10 + s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( hm . has ( val ) ) { hm . set ( val , hm . get ( val ) + 1 ) ; } else { hm . set ( val , 1 ) ; } } for ( let [ Key , Value ] of hm . entries ( ) ) { let key = Key ; let value = Value ; if ( value > 1 ) document . write ( key + " " + value + " " ) ; } } let input = " " ; printNum ( input ) ;
Count occurrences of a substring recursively | Recursive function to count the number of occurrences of " hi " in str . ; Base Case ; Recursive Case Checking if the first substring matches ; Otherwise , return the count from the remaining index ; Driver function
function countSubstrig ( str1 , str2 ) { let n1 = str1 . length ; let n2 = str2 . length ; if ( n1 == 0 n1 < n2 ) return 0 ; if ( str1 . substr ( 0 , n2 ) == ( str2 ) ) return countSubstrig ( str1 . substr ( n2 - 1 ) , str2 ) + 1 ; return countSubstrig ( str1 . substr ( n2 - 1 ) , str2 ) ; } let str1 = " " , str2 = " " ; document . write ( countSubstrig ( str1 , str2 ) , ' ' ) ; str1 = " " , str2 = " " ; document . write ( countSubstrig ( str1 , str2 ) , ' ' ) ;
Check if a binary string contains all permutations of length k | Javascript program to Check If a String Contains All Binary Codes of Size K ; Unordered map of type string ; Driver code
function hasAllcodes ( s , k ) { let us = new Set ( ) ; for ( let i = 0 ; i + k <= s . length ; i ++ ) { us . add ( s . substring ( i , i + k ) ) ; } return ( us . size == ( 1 << k ) ) ; } let s = " " ; let k = 2 ; if ( hasAllcodes ( s , k ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Add n binary strings | This function adds two binary strings and return result as a third string ; Initialize result ; Initialize digit sum ; Traverse both strings starting from last characters ; Compute sum of last digits and carry ; If current digit sum is 1 or 3 , add 1 to result ; Compute carry ; Move to next digits ; function to add n binary strings ; Driver program
function addBinaryUtil ( a , b ) { var result = " " ; var s = 0 ; var i = a . length - 1 , j = b . length - 1 ; while ( i >= 0 j >= 0 s == 1 ) { s += ( ( i >= 0 ) ? a . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) : 0 ) ; s += ( ( j >= 0 ) ? b . charCodeAt ( j ) - ' ' . charCodeAt ( 0 ) : 0 ) ; result = String . fromCharCode ( ( s % 2 == 1 ? 1 : 0 ) + ' ' . charCodeAt ( 0 ) ) + result ; s = parseInt ( s / 2 ) ; i -- ; j -- ; } return result ; } function addBinary ( arr , n ) { var result = " " ; for ( var i = 0 ; i < n ; i ++ ) result = addBinaryUtil ( result , arr [ i ] ) ; return result ; } var arr = [ " " , " " , " " ] ; var n = arr . length ; document . write ( addBinary ( arr , n ) ) ;
Reverse String according to the number of words | Reverse the letters of the word ; Temporary variable to store character ; Swapping the first and last character ; This function forms the required string ; Checking the number of words present in string to reverse ; Reverse the letter of the words ; Driver Code
function reverse ( str , start , end ) { let temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } } function reverseletter ( str , start , end ) { let wstart , wend ; for ( wstart = wend = start ; wend < end ; wend ++ ) { if ( str [ wend ] == ' ' ) { continue ; } while ( wend <= end && str [ wend ] != ' ' ) { wend ++ ; } wend -- ; reverse ( str , wstart , wend ) ; } } let str = " " . split ( " " ) ; reverseletter ( str , 0 , str . length - 1 ) ; document . write ( ( str ) . join ( " " ) ) ;
Minimum bit changes in Binary Circular array to reach a index | finding which path have minimum flip bit and the minimum flip bits ; concatenating given string to itself , to make it circular ; check x is greater than y . marking if output need to be opposite . ; iterate Clockwise ; if current bit is not equal to next index bit . ; iterate Anti - Clockwise ; if current bit is not equal to next index bit . ; Finding whether Clockwise or Anti - clockwise path take minimum flip . ; Driven Program
function minimumFlip ( s , x , y ) { s = s + s ; var isOpposite = false ; if ( x > y ) { swap ( x , y ) ; isOpposite = true ; } var valClockwise = 0 ; var cur = s [ x ] ; for ( var i = x ; i <= y ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valClockwise ++ ; } } var valAnticlockwise = 0 ; cur = s [ y ] ; x += s . length ; for ( var i = y ; i <= x ; i ++ ) { if ( s [ i ] != cur ) { cur = s [ i ] ; valAnticlockwise ++ ; } } if ( valClockwise <= valAnticlockwise ) { if ( ! isOpposite ) document . write ( " " + valClockwise + " " ) ; else document . write ( " " + valAnticlockwise + " " ) ; } else { if ( ! isOpposite ) document . write ( " " + valAnticlockwise + " " ) ; else document . write ( " " + valClockwise + " " ) ; } } var x = 0 , y = 8 ; var s = " " ; minimumFlip ( s , x , y ) ;
Prefixes with more a than b | Function to count prefixes ; calculating for string S ; count == 0 or when N == 1 ; when all characters are a or a - b == 0 ; checking for saturation of string after repetitive addition ; Driver Code
function prefix ( k , n ) { let a = 0 , b = 0 , count = 0 ; let i = 0 ; let len = k . length ; for ( i = 0 ; i < len ; i ++ ) { if ( k [ i ] == ' ' ) a ++ ; if ( k [ i ] == ' ' ) b ++ ; if ( a > b ) { count ++ ; } } if ( count == 0 n == 1 ) { document . write ( count + " " ) ; return 0 ; } if ( count == len a - b == 0 ) { document . write ( ( count * n ) + " " ) ; return 0 ; } let n2 = n - 1 , count2 = 0 ; while ( n2 != 0 ) { for ( i = 0 ; i < len ; i ++ ) { if ( k [ i ] == ' ' ) a ++ ; if ( k [ i ] == ' ' ) b ++ ; if ( a > b ) { count2 ++ ; } } count += count2 ; n2 -- ; if ( count2 == 0 ) break ; if ( count2 == len ) { count += ( n2 * count2 ) ; break ; } count2 = 0 ; } return count ; } let S = " " ; let N = 2 ; document . write ( prefix ( S , N ) + " " ) ; S = " " ; N = 3 ; document . write ( prefix ( S , N ) + " " ) ;
Pairs whose concatenation contain all digits | Javascript Program to find number of pairs whose concatenation contains all digits from 0 to 9. ; Function to return number of pairs whose concatenation contain all digits from 0 to 9 ; making the mask for each of the number . ; for each of the possible pair which can make OR value equal to 1023 ; finding the count of pair from the given numbers . ; Driver Code
let N = 20 ; function countPair ( str , n ) { let cnt = new Array ( 1 << 10 ) ; for ( let i = 0 ; i < cnt . length ; i ++ ) { cnt [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { let mask = 0 ; for ( let j = 0 ; j < str [ i ] . length ; ++ j ) mask |= ( 1 << ( str [ i ] [ j ] - ' ' ) ) ; cnt [ mask ] ++ ; } let ans = 0 ; for ( let m1 = 0 ; m1 <= 1023 ; m1 ++ ) for ( let m2 = 0 ; m2 <= 1023 ; m2 ++ ) if ( ( m1 m2 ) == 1023 ) { ans += ( ( m1 == m2 ) ? ( cnt [ m1 ] * ( cnt [ m1 ] - 1 ) ) : ( cnt [ m1 ] * cnt [ m2 ] ) ) ; } return Math . floor ( ans / 2 ) ; } let n = 5 ; let st = [ " " , " " , " " , " " , " " ] ; document . write ( countPair ( st , n ) )
Count binary strings with twice zeros in first half | pre define some constant ; global values for pre computation ; function to print number of required string ; calculate answer using proposed algorithm ; Driver code
var mod = 1000000007 var max = 1001 var nCr = Array . from ( Array ( 1003 ) , ( ) => Array ( 1003 ) ) ; function preComputeCoeff ( ) { for ( var i = 0 ; i < max ; i ++ ) { for ( var j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = ( nCr [ i - 1 ] [ j - 1 ] + nCr [ i - 1 ] [ j ] ) % mod ; } } } function computeStringCount ( N ) { var n = N / 2 ; var ans = 0 ; for ( var i = 2 ; i <= n ; i += 2 ) ans = ( ans + ( ( nCr [ n ] [ i ] * nCr [ n ] [ i / 2 ] ) % mod ) ) % mod ; return ans ; } preComputeCoeff ( ) ; var N = 3 ; document . write ( computeStringCount ( N ) ) ;
Count of ' GFG ' Subsequences in the given string | Javascript Program to find the " GFG " subsequence in the given string ; Print the count of " GFG " subsequence in the string ; Traversing the given string ; If the character is ' G ' , increment the count of ' G ' , increase the result and update the array . ; If the character is ' F ' , increment the count of ' F ' and update the array . ; Ignore other character . ; Driven Program
var MAX = 100 ; function countSubsequence ( s , n ) { var cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( var i = 0 ; i < n ; i ++ ) { switch ( s [ i ] ) { case ' ' : cntG ++ ; result += C ; break ; case ' ' : cntF ++ ; C += cntG ; break ; default : continue ; } } document . write ( result ) ; } var s = " " ; var n = ( s . length ) ; countSubsequence ( s , n ) ;