codeparrot/xlcost-text-to-code huggingface dataset:
Graph implementation using STL for competitive programming | Set 2 ( Weighted graph ) | To add an edge ; Print adjacency list representaion ot graph ; Driver code
function addEdge ( adj , u , v , wt ) { adj [ u ] . push ( [ v , wt ] ) ; adj [ v ] . push ( [ u , wt ] ) ; return adj ; } function printGraph ( adj , V ) { let v = 0 , w = 0 ; for ( let u = 0 ; u < V ; u ++ ) { document . write ( " " + u + " " ) ; for ( let it = 0 ; it < adj [ u ] . length ; it ++ ) { v = adj [ u ] [ it ] [ 0 ] ; w = adj [ u ] [ it ] [ 1 ] ; document . write ( " " + v + " " + w + " " ) } } } let V = 5 ; let adj = new Array ( V ) ; for ( let i = 0 ; i < V ; i ++ ) { adj [ i ] = [ ] ; } adj = addEdge ( adj , 0 , 1 , 10 ) adj = addEdge ( adj , 0 , 4 , 20 ) adj = addEdge ( adj , 1 , 2 , 30 ) adj = addEdge ( adj , 1 , 3 , 40 ) adj = addEdge ( adj , 1 , 4 , 50 ) adj = addEdge ( adj , 2 , 3 , 60 ) adj = addEdge ( adj , 3 , 4 , 70 ) printGraph ( adj , V ) ;
K Centers Problem | Set 1 ( Greedy Approximate Algorithm ) | Javascript program for the above approach ; Index of city having the maximum distance to it 's  closest center ; Updating the distance of the cities to their closest centers ; Updating the index of the city with the maximum distance to it 's closest center ; Printing the maximum distance of a city to a center that is our answer ; Printing the cities that were chosen to be made centers ; Driver Code ; Function Call
function maxindex ( dist , n ) { let mi = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( dist [ i ] > dist [ mi ] ) mi = i ; } return mi ; } function selectKcities ( n , weights , k ) { let dist = new Array ( n ) ; let centers = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { dist [ i ] = Number . MAX_VALUE ; } let max = 0 ; for ( let i = 0 ; i < k ; i ++ ) { centers . push ( max ) ; for ( let j = 0 ; j < n ; j ++ ) { dist [ j ] = Math . min ( dist [ j ] , weights [ max ] [ j ] ) ; } max = maxindex ( dist , n ) ; } document . write ( dist [ max ] + " " ) ; for ( let i = 0 ; i < centers . length ; i ++ ) { document . write ( centers [ i ] + " " ) ; } document . write ( " " ) ; } let n = 4 ; let weights = [ [ 0 , 4 , 8 , 5 ] , [ 4 , 0 , 10 , 7 ] , [ 8 , 10 , 0 , 9 ] , [ 5 , 7 , 9 , 0 ] ] let k = 2 selectKcities ( n , weights , k )
Number of Triangles in an Undirected Graph | Number of vertices in the graph ; Utility function for matrix multiplication ; Utility function to calculate trace of a matrix ( sum ofdiagnonal elements ) ; Utility function for calculating number of triangles in graph ; To Store graph ^ 2 ; To Store graph ^ 3 ; Initialising aux matrices with 0 ; aux2 is graph ^ 2 now printMatrix ( aux2 ) ; ; After this multiplication aux3 is graph ^ 3 printMatrix ( aux3 ) ; ; Driver code
let V = 4 ; function multiply ( A , B , C ) { for ( let i = 0 ; i < V ; i ++ ) { for ( let j = 0 ; j < V ; j ++ ) { C [ i ] [ j ] = 0 ; for ( let k = 0 ; k < V ; k ++ ) C [ i ] [ j ] += A [ i ] [ k ] * B [ k ] [ j ] ; } } } function getTrace ( graph ) { let trace = 0 ; for ( let i = 0 ; i < V ; i ++ ) trace += graph [ i ] [ i ] ; return trace ; } function triangleInGraph ( graph ) { let aux2 = new Array ( V ) ; let aux3 = new Array ( V ) ; for ( let i = 0 ; i < V ; ++ i ) { aux2 [ i ] = new Array ( V ) ; aux3 [ i ] = new Array ( V ) ; for ( let j = 0 ; j < V ; ++ j ) { aux2 [ i ] [ j ] = aux3 [ i ] [ j ] = 0 ; } } multiply ( graph , graph , aux2 ) ; multiply ( graph , aux2 , aux3 ) ; let trace = getTrace ( aux3 ) ; return ( trace / 6 ) ; } let graph = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 0 , 1 , 1 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 0 ] ] ; document . write ( " " + triangleInGraph ( graph ) ) ;
Count subsequences in first string which are anagrams of the second string | JavaScript implementation to count subsequences in first string which are anagrams of the second string ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- -- * 1 ] ; function to count subsequences in first string which are anagrams of the second string ; hash tables to store frequencies of each character ; store frequency of each character of ' str1' ; store frequency of each character of ' str2' ; to store the total count of subsequences ; if character ( i + ' a ' ) exists in ' str2' ; if this character ' s ▁ frequency ▁ ▁ in ▁ ' str2 ' ▁ in ▁ less ▁ than ▁ or ▁ ▁ equal ▁ to ▁ its ▁ frequency ▁ in ▁ ▁ ' str1 ' ▁ then ▁ accumulate ▁ its ▁ ▁ contribution ▁ to ▁ the ▁ count ▁ ▁ of ▁ subsequences . ▁ If ▁ its ▁ ▁ frequency ▁ in ▁ ' str1 ' ▁ is ▁ ' n ' ▁ ▁ and ▁ in ▁ ' str2 ' ▁ is ▁ ' r ', then  its contribution will be nCr,  where C is the binomial  coefficient. ; else return 0 as there could be no subsequence which is an anagram of ' str2' ; required count of subsequences ; Driver program to test above
var SIZE = 26 function binomialCoeff ( n , k ) { var res = 1 ; if ( k > n - k ) k = n - k ; for ( var i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } function countSubsequences ( str1 , str2 ) { var freq1 = Array ( SIZE ) . fill ( 0 ) , freq2 = Array ( SIZE ) . fill ( 0 ) ; var n1 = str1 . length ; var n2 = str2 . length ; for ( var i = 0 ; i < n1 ; i ++ ) freq1 [ str1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < n2 ; i ++ ) freq2 [ str2 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; var count = 1 ; for ( var i = 0 ; i < SIZE ; i ++ ) if ( freq2 [ i ] != 0 ) { if ( freq2 [ i ] <= freq1 [ i ] ) count = count * binomialCoeff ( freq1 [ i ] , freq2 [ i ] ) ; else return 0 ; } return count ; } var str1 = " " ; var str2 = " " ; document . write ( " " + countSubsequences ( str1 , str2 ) ) ;
Subsequence queries after removing substrings | arrays to store results of preprocessing ; function to preprocess the strings ; initialize it as 0. ; store subsequence count in forward direction ; store number of matches till now ; store subsequence count in backward direction ; store number of matches till now ; function that gives the output ; length of remaining String A is less than B 's length ; Driver Code ; two queries
let fwd = new Array ( 100 ) ; let bwd = new Array ( 100 ) ; function preProcess ( a , b ) { let n = a . length ; let j = 0 ; for ( let i = 1 ; i <= a . length ; i ++ ) { if ( j < b . length && a [ i - 1 ] == b [ j ] ) { j ++ ; } fwd [ i ] = j ; } j = 0 ; for ( let i = a . length ; i >= 1 ; i -- ) { if ( j < b . length && a [ i - 1 ] == b [ b . length - j - 1 ] ) { j ++ ; } bwd [ i ] = j ; } } function query ( a , b , x , y ) { if ( ( x - 1 + a . length - y ) < b . length ) { document . write ( " " ) ; return ; } if ( fwd [ x - 1 ] + bwd [ y + 1 ] >= b . length ) { document . write ( " " ) ; } else { document . write ( " " ) ; } } let a = " " , b = " " ; preProcess ( a , b ) ; let x = 2 , y = 5 ; query ( a , b , x , y ) ; x = 3 ; y = 6 ; query ( a , b , x , y ) ;
Count subsequence of length three in a given string | Function to find number of occurrences of a subsequence of length three in a string ; Variable to store no of occurrences ; Loop to find first character ; Loop to find 2 nd character ; Loop to find 3 rd character ; Increment count if subsequence is found ; Driver code
function findOccurrences ( str1 , substr1 ) { let counter = 0 ; for ( let i = 0 ; i < str1 . length ; i ++ ) { if ( str1 [ i ] == substr1 [ 0 ] ) { for ( let j = i + 1 ; j < str1 . length ; j ++ ) { if ( str1 [ j ] == substr1 [ 1 ] ) { for ( let k = j + 1 ; k < str1 . length ; k ++ ) { if ( str1 [ k ] == substr1 [ 2 ] ) counter ++ ; } } } } } return counter ; } let str1 = " " ; let substr1 = " " ; document . write ( findOccurrences ( str1 , substr1 ) ) ;
Count subsequence of length three in a given string | Function to find number of occurrences of a subsequence of length three in a string ; calculate length of string ; auxiliary array to store occurrences of first character ; auxiliary array to store occurrences of third character ; calculate occurrences of first character upto ith index from left ; calculate occurrences of third character upto ith index from right ; variable to store total number of occurrences ; loop to find the occurrences of middle element ; if middle character of subsequence is found in the string ; multiply the total occurrences of first character before middle character with the total occurrences of third character after middle character ; Driver function
function findOccurrences ( str , substr ) { let n = str . length ; let preLeft = new Array ( n ) ; preLeft . fill ( 0 ) ; let preRight = new Array ( n ) ; preRight . fill ( 0 ) ; if ( str [ 0 ] == substr [ 0 ] ) preLeft [ 0 ] ++ ; for ( let i = 1 ; i < n ; i ++ ) { if ( str [ i ] == substr [ 0 ] ) preLeft [ i ] = preLeft [ i - 1 ] + 1 ; else preLeft [ i ] = preLeft [ i - 1 ] ; } if ( str [ n - 1 ] == substr [ 2 ] ) preRight [ n - 1 ] ++ ; for ( let i = n - 2 ; i >= 0 ; i -- ) { if ( str [ i ] == substr [ 2 ] ) preRight [ i ] = preRight [ i + 1 ] + 1 ; else preRight [ i ] = preRight [ i + 1 ] ; } let counter = 0 ; for ( let i = 1 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ 1 ] ) { let total = preLeft [ i - 1 ] * preRight [ i + 1 ] ; counter += total ; } } return counter ; } let str = " " ; let substr = " " ; document . write ( findOccurrences ( str , substr ) ) ;
Form the largest palindromic number using atmost two swaps | function to form the largest palindromic number using atmost two swaps ; if length of number is less than '3' then no higher palindromic number can be formed ; find the index of last digit in the 1 st half of ' num ' ; as only the first half of ' num [ ] ' is being considered , therefore for the rightmost digit in the first half of ' num [ ] ' , there will be no greater right digit ; index of the greatest right digit till the current index from the right direction ; traverse the array from second right element in the first half of ' num [ ] ' up to the left element ; if ' num [ i ] ' is less than the greatest digit encountered so far ; there is no greater right digit for ' num [ i ] ' ; update ' right ' index ; traverse the ' rightMax [ ] ' array from left to right ; if for the current digit , greater right digit exists then swap it with its greater right digit and also perform the required swap operation in the right halft of ' num [ ] ' to maintain palindromic property , then break ; performing the required swap operations ;  ; Driver code ; required largest palindromic number
function largestPalin ( num , n ) { if ( n <= 3 ) return ; let mid = Math . floor ( n / 2 ) - 1 ; let rightMax = new Array ( mid + 1 ) ; let right ; rightMax [ mid ] = - 1 ; right = mid ; for ( let i = mid - 1 ; i >= 0 ; i -- ) { if ( num [ i ] < num [ right ] ) rightMax [ i ] = right ; else { rightMax [ i ] = - 1 ; right = i ; } } for ( let i = 0 ; i <= mid ; i ++ ) { if ( rightMax [ i ] != - 1 ) { swap ( num , i , rightMax [ i ] ) ; swap ( num , n - i - 1 , n - rightMax [ i ] - 1 ) ; break ; } } } function swap ( arr , i , j ) { let temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } let num = " " . split ( " " ) ; let n = num . length ; largestPalin ( num , n ) ; document . write ( " " + ( num ) . join ( " " ) ) ;
Mirror characters of a string | Function which take the given string and the position from which the reversing shall be done and returns the modified string ; Creating a string having reversed alphabetical order ; The string up to the point specified in the question , the string remains unchanged and from the point up to the length of the string , we reverse the alphabetical order ; Driver function
function compute ( str , n ) { let reverseAlphabet = " " ; let l = str . length ; let answer = " " ; for ( let i = 0 ; i < n ; i ++ ) answer = answer + str [ i ] ; for ( let i = n ; i < l ; i ++ ) answer = answer + reverseAlphabet [ str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ; return answer ; } let str = " " ; let n = 4 ; document . write ( compute ( str , n - 1 ) ) ;
Lexicographically smallest string whose hamming distance from given string is exactly K | javascript program to find Lexicographically smallest string whose hamming distance from given string is exactly K ; If k is 0 , output input string ; Copying input string into output string ; Traverse all the character of the string ; If current character is not ' a ' ; copy character ' a ' to output string ; str2 [ i ] = ' a ' ; ; If hamming distance became k , break ; ; If k is less than p ; Traversing string in reverse order ; Driven Program
function findString ( str , n , k ) { if ( k == 0 ) { document . write ( str ) ; ; return ; } var str2 = str ; var p = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( str2 . charAt ( i ) != ' ' ) { str2 = str2 . substring ( 0 , i ) + ' ' + str2 . substring ( i + 1 ) ; p ++ ; if ( p == k ) break ; } } if ( p < k ) { for ( i = n - 1 ; i >= 0 ; i -- ) if ( str . charAt ( i ) == ' ' ) { str2 = str2 . substring ( 0 , i ) + ' ' + str2 . substring ( i + 1 ) ; p ++ ; if ( p == k ) break ; } } document . write ( str2 ) ; } var str = " " ; var n = str . length ; var k = 2 ; findString ( str , n , k ) ;
Lexicographically first alternate vowel and consonant string | JavaScript implementation of lexicographically first alternate vowel and consonant String ; ' ch ' is vowel or not ; create alternate vowel and consonant String str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required lexicographically first alternate vowel and consonant String ; hash table to store frequencies of each character in ' str ' ; initialize all elements of char_freq [ ] to 0 ; count vowels ; count consonants ; update frequency of ' ch ' in char_freq [ ] ; no such String can be formed ; form the vowel String ' vstr ' and consonant String ' cstr ' which contains characters in lexicographical order ; remove first character of vowel String then create alternate String with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant String then create alternate String with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating String with consonant ; start creating String with vowel ; Driver code
let SIZE = 26 ; function isVowel ( ch ) { if ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) { return true ; } return false ; } function createAltStr ( str1 , str2 , start , l ) { let finalStr = " " ; for ( let i = 0 , j = start ; j < l ; i ++ , j ++ ) { finalStr = ( finalStr + str1 [ i ] ) + str2 [ j ] ; } return finalStr ; } function findAltStr ( str ) { let char_freq = new Array ( SIZE ) ; for ( let i = 0 ; i < SIZE ; i ++ ) char_freq [ i ] = 0 ; let nv = 0 , nc = 0 ; let vstr = " " , cstr = " " ; let l = str . length ; for ( let i = 0 ; i < l ; i ++ ) { let ch = str [ i ] ; if ( isVowel ( ch ) ) { nv ++ ; } else { nc ++ ; } char_freq [ ch . charCodeAt ( 0 ) - 97 ] ++ ; } if ( Math . abs ( nv - nc ) >= 2 ) { return " " ; } for ( let i = 0 ; i < SIZE ; i ++ ) { let ch = String . fromCharCode ( i + 97 ) ; for ( let j = 1 ; j <= char_freq [ i ] ; j ++ ) { if ( isVowel ( ch ) ) { vstr += ch ; } else { cstr += ch ; } } } if ( nv > nc ) { return ( vstr [ 0 ] + createAltStr ( cstr , vstr , 1 , nv ) ) ; } if ( nc > nv ) { return ( cstr [ 0 ] + createAltStr ( vstr , cstr , 1 , nc ) ) ; } if ( cstr [ 0 ] < vstr [ 0 ] ) { return createAltStr ( cstr , vstr , 0 , nv ) ; } return createAltStr ( vstr , cstr , 0 , nc ) ; } let str = " " ; document . write ( findAltStr ( str ) ) ;
Decode an Encoded Base 64 String to ASCII String | JavaScript Program to decode a base64 Encoded String back to ASCII String ; char_set = " ABCDEFGHIJKLMNOPQRSTUVWXYZ ▁ abcdefghijklmnopqrstuvwxyz0123456789 + / " ; stores the bitstream . ; count_bits stores current number of bits in num . ; selects 4 characters from encoded String at a time . find the position of each encoded character in char_set and stores in num . ; make space for 6 bits . ; encoded [ i + j ] = ' E ' , ' E ' - ' A ' = 5 ' E ' has 5 th position in char_set . ; encoded [ i + j ] = ' e ' , ' e ' - ' a ' = 5 , 5 + 26 = 31 , ' e ' has 31 st position in char_set . ; encoded [ i + j ] = '8' , '8' - '0' = 8 8 + 52 = 60 , '8' has 60 th position in char_set . ; ' + ' occurs in 62 nd position in char_set . ; ' / ' occurs in 63 rd position in char_set . ; ( str [ i + j ] == ' = ' ) remove 2 bits to delete appended bits during encoding . ; 255 in binary is 11111111 ; Driver code ; Do not count last null character .
let SIZE = 100 ; function base64Decoder ( encoded , len_str ) { let decoded_String ; decoded_String = new Array ( SIZE ) ; let i , j , k = 0 ; let num = 0 ; let count_bits = 0 ; for ( i = 0 ; i < len_str ; i += 4 ) { num = 0 ; count_bits = 0 ; for ( j = 0 ; j < 4 ; j ++ ) { if ( encoded [ i + j ] != ' ' ) { num = num << 6 ; count_bits += 6 ; } if ( encoded [ i + j ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && encoded [ i + j ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) num = num | ( encoded [ i + j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; else if ( encoded [ i + j ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && encoded [ i + j ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) num = num | ( encoded [ i + j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 26 ) ; else if ( encoded [ i + j ] . charCodeAt ( 0 ) >= ' ' . charCodeAt ( 0 ) && encoded [ i + j ] . charCodeAt ( 0 ) <= ' ' . charCodeAt ( 0 ) ) num = num | ( encoded [ i + j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 52 ) ; else if ( encoded [ i + j ] == ' ' ) num = num | 62 ; else if ( encoded [ i + j ] == ' ' ) num = num | 63 ; else { num = num >> 2 ; count_bits -= 2 ; } } while ( count_bits != 0 ) { count_bits -= 8 ; decoded_String [ k ++ ] = String . fromCharCode ( ( num >> count_bits ) & 255 ) ; } } return ( decoded_String ) ; } let encoded_String = " " . split ( " " ) ; let len_str = encoded_String . length ; len_str -= 1 ; document . write ( " " + ( encoded_String ) . join ( " " ) + " " ) ; document . write ( " " + base64Decoder ( encoded_String , len_str ) . join ( " " ) + " " ) ;
Print all possible strings that can be made by placing spaces | Function to print all subsequences ; Driver code
function printSubsequences ( s ) { let str = s . split ( ' ' ) ; let n = str . length ; let opsize = Math . pow ( 2 , n - 1 ) ; for ( let counter = 0 ; counter < opsize ; counter ++ ) { for ( let j = 0 ; j < n ; j ++ ) { document . write ( str [ j ] ) ; if ( ( counter & ( 1 << j ) ) > 0 ) document . write ( " " ) ; } document . write ( " " ) ; } } let str = " " ; printSubsequences ( str ) ;
Find n | Javascript program to print n - th permutation ; Utility for calculating factorial ; Function for nth permutation ; length of given string ; Count frequencies of all characters ; out string for output string ; Iterate till sum equals n ; We update both n and sum in this loop . ; Check for characters present in freq [ ] ; Remove character ; calculate sum after fixing a particular char ; if sum > n fix that char as present char and update sum and required nth after fixing char at that position ; if sum < n , add character back ; if sum == n means this char will provide its greatest permutation as nth permutation ; append string termination character and print result ; Driver code
let MAX_CHAR = 26 ; let MAX_FACT = 20 ; let fact = new Array ( MAX_FACT ) ; function precomputeFactorirals ( ) { fact [ 0 ] = 1 ; for ( let i = 1 ; i < MAX_FACT ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } function nPermute ( str , n ) { precomputeFactorirals ( ) ; let len = str . length ; let freq = new Array ( MAX_CHAR ) ; for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { freq [ i ] = 0 ; } for ( let i = 0 ; i < len ; i ++ ) freq [ str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; let out = " " ; let sum = 10 ; let k = 0 ; while ( sum >= n ) { for ( let i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; sum = 0 ; let xsum = fact [ len - 1 - k ] ; for ( let j = 0 ; j < MAX_CHAR ; j ++ ) xsum = Math . floor ( xsum / fact [ freq [ j ] ] ) ; sum += xsum ; if ( sum >= n ) { out += String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; k ++ ; n -= ( sum - xsum ) ; break ; } if ( sum < n ) freq [ i ] ++ ; } } for ( let i = MAX_CHAR - 1 ; k < len && i >= 0 ; i -- ) if ( freq [ i ] != 0 ) { out += String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ; freq [ i ++ ] -- ; } document . write ( out ) ; } let n = 2 ; let str = " " ; nPermute ( str , n ) ;
Minimum number of deletions so that no two consecutive are same | Function for counting deletions ; If two consecutive characters are the same , delete one of them . ; Driver code ; Function call to prvar answer
function countDeletions ( s ) { var ans = 0 ; var str = s ; for ( var i = 0 ; i < str . length - 1 ; i ++ ) if ( str [ i ] == str [ i + 1 ] ) ans ++ ; return ans ; } var str = " " ; document . write ( countDeletions ( str ) ) ;
Count words that appear exactly two times in an array of words | Returns count of words with frequency exactly 2. ; Driver code
function countWords ( str , n ) { var m = new Map ( ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( m . has ( str [ i ] ) ) m . set ( str [ i ] , m . get ( str [ i ] ) + 1 ) else m . set ( str [ i ] , 1 ) } var res = 0 ; m . forEach ( ( value , key ) => { if ( ( value == 2 ) ) res ++ ; } ) ; return res ; } var s = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; var n = s . length ; document . write ( countWords ( s , n ) ) ;
Nth Even length Palindrome | Function to find nth even length Palindrome ; string r to store resultant palindrome . Initialize same as s ; In this loop string r stores reverse of string s after the string s in consecutive manner ; Driver code ; Function call
function evenlength ( n ) { let res = n ; for ( let j = n . length - 1 ; j >= 0 ; -- j ) res += n [ j ] ; return res ; } let n = " " ; document . write ( evenlength ( n ) ) ;
Program to count occurrence of a given character in a string | package whatever do not write package name here ; base case ; ; checking if the first character of the given string is that character or not ; this will count the occurrence of given character in the string from index 1 to the last index of the string ; Driver code
function countinString ( ch , s ) { if ( s . length == 0 ) return 0 ; var count = 0 ; if ( s [ 0 ] == ch ) count ++ ; count += countinString ( ch , s . substring ( 1 ) ) ; return count ; } var str = " " ; var c = ' ' ; document . write ( countinString ( c , str ) ) ;
Determine if a string has all Unique Characters | Javascript program to illustrate string with unique characters using brute force technique ; If at any time we encounter 2 same characters , return false ; If no duplicate characters encountered , return true ; Driver code
function uniqueCharacters ( str ) { for ( let i = 0 ; i < str . length ; i ++ ) for ( let j = i + 1 ; j < str . length ; j ++ ) if ( str [ i ] == str [ j ] ) return false ; return true ; } let input = " " ; if ( uniqueCharacters ( input ) == true ) document . write ( " " + input + " " + " " ) ; else document . write ( " " + input + " " ) ;
Check if given string can be split into four distinct strings | Return true if both strings are equal ; Return if the given string can be split or not . ; Brute Force ; Making 4 string from the given string ; Checking if they are distinct or not . ; Driver code
function strcheck ( s1 , s2 ) { if ( s1 . localeCompare ( s2 ) != 0 ) return false ; return true ; } function check ( s ) { if ( s . length >= 10 ) return true ; for ( let i = 1 ; i < s . length ; i ++ ) { for ( let j = i + 1 ; j < s . length ; j ++ ) { for ( let k = j + 1 ; k < s . length ; k ++ ) { let s1 = " " , s2 = " " , s3 = " " , s4 = " " ; s1 = s . substring ( 0 , i ) ; s2 = s . substring ( i , i + j - i ) ; s3 = s . substring ( j , j + k - j ) ; s4 = s . substring ( k , k + s . length - k ) ; if ( strcheck ( s1 , s2 ) && strcheck ( s1 , s3 ) && strcheck ( s1 , s4 ) && strcheck ( s2 , s3 ) && strcheck ( s2 , s4 ) && strcheck ( s3 , s4 ) ) return true ; } } } return false ; } let str = " " ; if ( check ( str ) ) document . write ( " " ) ; else document . write ( " " ) ;