codeparrot/xlcost-text-to-code huggingface dataset:
Longest Subsequence of a String containing only Consonants | Returns true if x is consonants . ; Function to check whether a character is consonants or not ; Function to find the longest subsequence which contain all consonants ; Driver code ; Function call
function isConsonants ( x ) { x = x . toLowerCase ( ) ; return ! ( x === " " x === " " x === " " x === " " x === " " ) ; } function longestConsonantsSubsequence ( str ) { var answer = " " ; var n = str . length ; for ( var i = 0 ; i < n ; i ++ ) { if ( isConsonants ( str [ i ] ) ) { answer += str [ i ] ; } } return answer ; } var str = " " ; document . write ( longestConsonantsSubsequence ( str ) + " " ) ;
Find the length of the longest subsequence with first K alphabets having same frequency | Function to return the length of the longest subsequence with first K alphabets having same frequency ; Map to store frequency of all characters in the string ; Variable to store the frequency of the least frequent of first K alphabets ; Driver code
function lengthOfSubsequence ( str , K ) { var mp = new Map ( ) ; str . split ( ' ' ) . forEach ( ch => { if ( mp . has ( ch ) ) mp . set ( ch , mp . get ( ch ) + 1 ) else mp . set ( ch , 1 ) } ) ; var minimum = mp . get ( ' ' ) ; for ( var i = 1 ; i < K ; i ++ ) { minimum = Math . min ( minimum , mp . get ( String . fromCharCode ( i + ' ' . charCodeAt ( 0 ) ) ) ) ; } return minimum * K ; } var str = " " ; var K = 3 ; document . write ( lengthOfSubsequence ( str , K ) ) ;
Digital Root of a given large integer using Recursion | Function to convert given sum into string ; Loop to extract digit one by one from the given sum and concatenate into the string ; Type casting for concatenation ; Return converted string ; Function to get individual digit sum from string ; Loop to get individual digit sum ; Function call to convert sum into string ; Function to calculate the digital root of a very large number ; Base condition ; Function call to get individual digit sum ; Recursive function to get digital root of a very large number ; Driver code ; Function to print final digit
function convertToString ( sum ) { let str = " " ; while ( sum > 0 ) { str = str + String . fromCharCode ( ( sum % 10 ) + ' ' . charCodeAt ( ) ) ; sum = parseInt ( sum / 10 , 10 ) ; } return str ; } function GetIndividulaDigitSum ( str , len ) { let sum = 0 ; for ( let i = 0 ; i < len ; i ++ ) { sum = sum + str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; } return convertToString ( sum ) ; } function GetDigitalRoot ( str ) { if ( str . length == 1 ) { return ( str [ 0 ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) ; } str = GetIndividulaDigitSum ( str , str . length ) ; return GetDigitalRoot ( str ) ; } let str = " " ; document . write ( GetDigitalRoot ( str ) ) ;
Count the Number of matching characters in a pair of strings | Function to count the matching characters ; Traverse the string 1 char by char ; This will check if str1 [ i ] is present in str2 or not str2 . find ( str1 [ i ] ) returns - 1 if not found otherwise it returns the starting occurrence index of that character in str2 ; Driver code
function count ( str1 , str2 ) { var c = 0 ; for ( var i = 0 ; i < str1 . length ; i ++ ) { if ( str2 . includes ( str1 [ i ] ) ) c += 1 ; } document . write ( " " + + parseInt ( c ) ) ; } var str1 = " " ; var str2 = " " ; count ( str1 , str2 ) ;
Check if count of Alphabets and count of Numbers are equal in the given String | Function to count the number of alphabets ; Counter to store the number of alphabets in the string ; Every character in the string is iterated ; To check if the character is an alphabet or not ; Function to count the number of numbers ; Counter to store the number of alphabets in the string ; Every character in the string is iterated ; To check if the character is a digit or not ; Function to check if the count of alphabets is equal to the count of numbers or not ; Driver code
function countOfLetters ( str ) { var letter = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( ( str [ i ] >= ' ' && str [ i ] <= ' ' ) || ( str [ i ] >= ' ' && str [ i ] <= ' ' ) ) letter ++ ; } return letter ; } function countOfNumbers ( str ) { var number = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] >= ' ' && str [ i ] <= ' ' ) number ++ ; } return number ; } function check ( str ) { if ( countOfLetters ( str ) == countOfNumbers ( str ) ) document . write ( " " ) ; else document . write ( " " ) ; } var str = " " ; check ( str ) ;
Minimum cost to traverse from one index to another in the String | function to find the minimum cost ; graph ; adjacency matrix ; initialising adjacency matrix ; creating adjacency list ; pushing left adjacent elelemt for index ' k ' ; pushing right adjacent element for index ' k ' ; queue to perform BFS ; visited array ; variable to store depth of BFS ; BFS ; number of elements in the current level ; inner loop ; current element ; popping queue ; base case ; checking if the current node is required node ; iterating through the current node ; updating depth ; input variables ; function to find the minimum cost
function findMinCost ( s , i , j ) { var gr = Array . from ( Array ( 26 ) , ( ) => new Array ( ) ) ; var edge = Array . from ( Array ( 26 ) , ( ) => Array ( 26 ) ) ; for ( var k = 0 ; k < 26 ; k ++ ) for ( var l = 0 ; l < 26 ; l ++ ) edge [ k ] [ l ] = 0 ; for ( var k = 0 ; k < s . length ; k ++ ) { if ( k - 1 >= 0 && ! edge [ s [ k ] . charCodeAt ( 0 ) - 97 ] [ s [ k - 1 ] . charCodeAt ( 0 ) - 97 ] ) gr [ s [ k ] . charCodeAt ( 0 ) - 97 ] . push ( s [ k - 1 ] . charCodeAt ( 0 ) - 97 ) , edge [ s [ k ] . charCodeAt ( 0 ) - 97 ] [ s [ k - 1 ] . charCodeAt ( 0 ) - 97 ] = 1 ; if ( k + 1 <= s . length - 1 && ! edge [ s [ k ] . charCodeAt ( 0 ) - 97 ] [ s [ k + 1 ] . charCodeAt ( 0 ) - 97 ] ) gr [ s [ k ] . charCodeAt ( 0 ) - 97 ] . push ( s [ k + 1 ] . charCodeAt ( 0 ) - 97 ) , edge [ s [ k ] . charCodeAt ( 0 ) - 97 ] [ s [ k + 1 ] . charCodeAt ( 0 ) - 97 ] = 1 ; } var q = [ ] ; q . push ( s [ i ] . charCodeAt ( 0 ) - 97 ) ; var v = Array ( 26 ) . fill ( 0 ) ; var d = 0 ; while ( q . length > 0 ) { var cnt = q . length ; while ( cnt -- > 0 ) { var curr = q [ 0 ] ; q . shift ( ) ; if ( v [ curr ] ) continue ; v [ curr ] = 1 ; if ( curr == s [ j ] . charCodeAt ( 0 ) - 97 ) return d ; for ( var it = 0 ; it < gr [ curr ] . length ; it ++ ) { q . push ( gr [ curr ] [ it ] ) ; } } d ++ ; } return - 1 ; } var s = " " ; var i = 0 ; var j = 4 ; document . write ( findMinCost ( s , i , j ) ) ;
Index of character depending on frequency count in string | JavaScript implementation of the approach ; Function to perform the queries ; L [ i ] [ j ] stores the largest i such that ith character appears exactly jth times in str [ 0. . . i ] ; F [ i ] [ j ] stores the smallest i such that ith character appears exactly jth times in str [ 0. . . i ] ; To store the frequency of each of the character of str ; Current character of str ; Update its frequency ; For every lowercase character of the English alphabet ; If it is equal to the character under consideration then update L [ ] [ ] and R [ ] [ ] as it is cnt [ j ] th occurrence of character k ; Only update L [ ] [ ] as k has not been occurred so only index has to be incremented ; Perform the queries ; Type 1 query ; Type 2 query ; Driver code ; Queries ; Perform the queries
let MAX = 26 ; function performQueries ( str , q , type , ch , freq ) { let n = str . length ; let L = new Array ( MAX ) ; let F = new Array ( MAX ) ; let cnt = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { L [ i ] = new Array ( n ) ; F [ i ] = new Array ( n ) ; cnt [ i ] = 0 ; for ( let j = 0 ; j < n ; j ++ ) { L [ i ] [ j ] = 0 ; F [ i ] [ j ] = 0 ; } } for ( let i = 0 ; i < n ; i ++ ) { let k = str [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ; cnt [ k ] ++ ; for ( let j = 0 ; j < MAX ; j ++ ) { if ( k == j ) { L [ j ] [ cnt [ j ] ] = i ; F [ j ] [ cnt [ j ] ] = i ; } else L [ j ] [ cnt [ j ] ] = L [ j ] [ cnt [ j ] ] + 1 ; } } for ( let i = 0 ; i < q ; i ++ ) { if ( type [ i ] == 1 ) { document . write ( L [ ch [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] [ freq [ i ] ] ) ; } else { document . write ( F [ ch [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] [ freq [ i ] ] ) ; } document . write ( " " ) ; } } let str = " " ; let type = [ 1 , 2 ] ; let ch = [ ' ' , ' ' ] ; let freq = [ 2 , 2 ] ; let q = type . length ; performQueries ( str , q , type , ch , freq ) ;
Number of index pairs such that s [ i ] and s [ j ] are anagrams | Function to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; To store count of strings ; Traverse all strings and store in the map ; Sort the string ; Store in the map ; To store the number of pairs ; Traverse through the map ; Count the pairs for each string ; Return the required answer ; Driver code ; Function call
function anagram_pairs ( s , n ) { let mp = new Map ( ) ; for ( let i = 0 ; i < n ; i ++ ) { let chArr = s [ i ] . split ( " " ) ; chArr . sort ( ) ; s [ i ] = chArr . join ( " " ) ; if ( mp . has ( s [ i ] ) ) { mp . set ( s [ i ] , mp . get ( s [ i ] ) + 1 ) } else { mp . set ( s [ i ] , 1 ) } } let ans = 0 ; for ( let i of mp ) { let k = i [ 1 ] ; ans += Math . floor ( ( k * ( k - 1 ) ) / 2 ) ; } return ans ; } let s = [ " " , " " , " " , " " , " " ] ; let n = s . length ; document . write ( anagram_pairs ( s , n ) ) ;
Insert a Character in a Rotated String | Function to insert the character ; To store last position where the insertion is done ; To store size of the string ; To store the modified string ; To store characters ; Add first character to the string ; Update the size ; Update the index of last insertion ; Insert all other characters to the string ; Take the character ; Take substring upto ind ; Take modulo value of k with the size of the string ; Check if we need to move to the start of the string ; If we don 't need to move to  start of the string ; Take substring from upto temp ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; If we need to move to start of the string ; Take substring which will before the inserted character ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; Return the required character ; Driver code ; Function call
function Insert ( arr , n , k ) { let ind = 0 ; let sz = 0 ; let s = " " ; let ch = arr [ 0 ] ; s += ch ; sz = 1 ; ind = 0 ; for ( let i = 1 ; i < n ; i ++ ) { ch = arr [ i ] ; let s1 = s . substring ( 0 , ind + 1 ) ; let temp = k % sz ; let ro = temp - Math . min ( temp , sz - ind - 1 ) ; if ( ro == 0 ) { let s2 = s . substring ( ind + 1 , ind + 1 + temp ) ; let s3 = s . substring ( ind + temp + 1 , sz ) ; s = s1 + s2 + ch + s3 ; ind = s1 . length + s2 . length ; sz = s . length ; } else { let s2 = s . substring ( 0 , ro ) ; let s3 = s . substring ( ro , sz ) ; s = s2 + ch + s3 ; ind = s2 . length ; sz = s . length ; } } if ( ind == 0 ) { return s [ sz - 1 ] ; } else { return s [ ind - 1 ] ; } } let arr = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; let k = 2 ; let n = arr . length ; document . write ( Insert ( arr , n , k ) ) ;
Queries to find the last non | Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string str [ l ... r ] ; Function to return the last non - repeating character ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array
let MAX = 256 ; let freq ; function preCalculate ( str , n ) { freq [ str [ 0 ] . charCodeAt ( 0 ) ] [ 0 ] = 1 ; for ( let i = 1 ; i < n ; i ++ ) { let ch = str [ i ] ; for ( let j = 0 ; j < MAX ; j ++ ) { let charToUpdate = String . fromCharCode ( j ) ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } function getFrequency ( ch , l , r ) { if ( l == 0 ) return freq [ ch . charCodeAt ( 0 ) ] [ r ] ; else return ( freq [ ch . charCodeAt ( 0 ) ] [ r ] - freq [ ch . charCodeAt ( 0 ) ] [ l - 1 ] ) ; } function lastNonRepeating ( str , n , l , r ) { for ( let i = r ; i >= l ; i -- ) { let ch = str [ i ] ; if ( getFrequency ( ch , l , r ) == 1 ) return ( " " + ch ) ; } return " " ; } let str = " " ; let n = str . length ; let queries = [ [ 2 , 9 ] , [ 2 , 3 ] , [ 0 , 12 ] ] ; let q = queries . length ; freq = new Array ( MAX ) ; for ( let i = 0 ; i < MAX ; i ++ ) { freq [ i ] = new Array ( n ) ; for ( let j = 0 ; j < n ; j ++ ) { freq [ i ] [ j ] = 0 ; } } preCalculate ( str , n ) ; for ( let i = 0 ; i < q ; i ++ ) { document . write ( lastNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) + " " ) ; }
Queries to answer the X | Function to pre - process the sub - strings in sorted order ; Generate all substrings ; Iterate to find all sub - strings ; Store the sub - string in the vector ; Sort the substrings lexicographically ; Driver code ; To store all the sub - strings ; Perform queries
function pre_process ( substrings , s ) { var n = s . length ; for ( var i = 0 ; i < n ; i ++ ) { var dup = " " ; for ( var j = i ; j < n ; j ++ ) { dup += s [ j ] ; substrings . push ( dup ) ; } } substrings . sort ( ) ; } var s = " " ; var substrings = [ ] ; pre_process ( substrings , s ) ; var queries = [ 1 , 5 , 10 ] ; var q = queries . length ; for ( var i = 0 ; i < q ; i ++ ) document . write ( substrings [ queries [ i ] - 1 ] + " " ) ;
Print the final string when minimum value strings get concatenated in every operation | Function that prints the final string ; Add all the strings and their corresponding values to the priority queue ; Take those two strings from the priority queue whose corresponding integer values are smaller and add them up as well as their values and add them back to the priority queue while there are more than a single element in the queue ; Get the minimum valued string ; Get the second minimum valued string ; Updated integer value ; Create new entry for the queue ; Add to the queue ; Print the only remaining string ; Driver code
function FindString ( str , num , n ) { var p = [ ] ; for ( var i = 0 ; i < n ; i ++ ) { var x = [ 0 , 0 ] ; x [ 0 ] = str [ i ] ; x [ 1 ] = num [ i ] ; p . push ( x ) ; } while ( p . length > 1 ) { var x = p [ p . length - 1 ] ; p . pop ( ) ; var y = p [ p . length - 1 ] ; p . pop ( ) ; var temp = x [ 1 ] + y [ 1 ] ; var sb = x [ 0 ] + y [ 0 ] ; var z = [ 0 , 0 ] ; z [ 1 ] = temp ; z [ 0 ] = sb ; p . push ( z ) ; } var z = p [ p . length - 1 ] ; p . pop ( ) ; document . write ( z [ 0 ] + " " ) ; } var str = [ " " , " " , " " ] ; var num = [ 2 , 3 , 7 ] ; var n = num . length ; FindString ( str , num , n ) ;
Minimum cost to modify a string | Function to return the minimum cost ; Initialize result ; To store the frequency of characters of the string ; Update the frequencies of the characters of the string ; Loop to check all windows from a - z where window size is K ; Starting index of window ; Ending index of window ; Check if the string contains character ; Check if the character is on left side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Check if the character is on right side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Find the minimum of all costs for modifying the string ; Loop to check all windows Here window contains characters before z and after z of window size K ; Starting index of window ; Ending index of window ; Check if the string contains character ; If characters are outside window find the cost for modifying character add value to count ; Find the minimum of all costs for modifying the string ; Driver code
function minCost ( str , K ) { var n = str . length ; var res = 999999999 , count = 0 , a , b ; var cnt = Array ( 27 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) cnt [ str [ i ] . charCodeAt ( 0 ) - 97 + 1 ] ++ ; for ( var i = 1 ; i < ( 26 - K + 1 ) ; i ++ ) { a = i ; b = i + K ; count = 0 ; for ( var j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= a && j >= b ) count = count + ( Math . min ( j - b , 25 - j + a + 1 ) ) * cnt [ j ] ; else if ( j <= a && j <= b ) count = count + ( Math . min ( a - j , 25 + j - b + 1 ) ) * cnt [ j ] ; } } res = Math . min ( res , count ) ; } for ( var i = 26 - K + 1 ; i <= 26 ; i ++ ) { a = i ; b = ( i + K ) % 26 ; count = 0 ; for ( var j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= b && j <= a ) count = count + ( Math . min ( j - b , a - j ) ) * cnt [ j ] ; } } res = Math . min ( res , count ) ; } return res ; } var str = " " ; var K = 2 ; document . write ( minCost ( str , K ) ) ;
Generate all binary strings of length n with sub | Utility function to print the given binary string ; This function will be called recursively to generate the next bit for given binary string according to its current state ; Base - case : if the generated binary string meets the required length and the pattern "01" appears twice ; nextbit needs to be 0 because each time we call the function recursively , we call 2 times for 2 cases : next bit is 0 or 1 The is to assure that the binary string is printed one time only ; Generate the next bit for str and call recursive ; Assign first bit ; The next generated bit will wither be 0 or 1 ; If pattern "01" occurrence is < 2 ; Set next bit ; If pattern "01" appears then increase the occurrence of pattern ; Else pattern "01" occurrence equals 2 ; If previous bit is 0 then next bit cannot be 1 ; Otherwise ; Driver code ; Length of the resulting strings must be at least 4 ; Generate all binary strings of length n with sub - string "01" appearing twice
function printBinStr ( str , len ) { for ( var i = 0 ; i < len ; i ++ ) { document . write ( str [ i ] ) ; } document . write ( " " ) ; } function generateBinStr ( str , len , currlen , occur , nextbit ) { if ( currlen == len ) { if ( occur == 2 && nextbit == 0 ) printBinStr ( str , len ) ; return ; } if ( currlen == 0 ) { str [ 0 ] = nextbit ; generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; } else { if ( occur < 2 ) { str [ currlen ] = nextbit ; if ( str [ currlen - 1 ] == 0 && nextbit == 1 ) { occur += 1 ; } generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; } else { if ( str [ currlen - 1 ] == 0 && nextbit == 1 ) { return ; } else { str [ currlen ] = nextbit ; generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; } } } } var n = 5 ; if ( n < 4 ) document . write ( - 1 ) ; else { var str = Array ( n ) ; generateBinStr ( str , n , 0 , 0 , 0 ) ; generateBinStr ( str , n , 0 , 0 , 1 ) ; }
Print last character of each word in a string | Function to print the last character of each word in the given string ; Now , last word is also followed by a space ; If current character is a space ; Then previous character must be the last character of some word ; Driver code
function printLastChar ( str ) { str = str + " " ; for ( var i = 1 ; i < str . length ; i ++ ) { if ( str [ i ] === " " ) document . write ( str [ i - 1 ] + " " ) ; } } var str = " " ; printLastChar ( str ) ;
Maximum length of balanced string after swapping and removal of characters | Javascript implementation of the approach Function to return the length of the longest balanced sub - string ; To store the count of parentheses ; Traversing the string ; Check type of parentheses and incrementing count for it ; Sum all pair of balanced parentheses ; Driven code
function maxBalancedStr ( s ) { var open1 = 0 , close1 = 0 ; var open2 = 0 , close2 = 0 ; var open3 = 0 , close3 = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { switch ( s . charAt ( i ) ) { case ' ' : open1 ++ ; break ; case ' ' : close1 ++ ; break ; case ' ' : open2 ++ ; break ; case ' ' : close2 ++ ; break ; case ' ' : open3 ++ ; break ; case ' ' : close3 ++ ; break ; } } var maxLen = 2 * Math . min ( open1 , close1 ) + 2 * Math . min ( open2 , close2 ) + 2 * Math . min ( open3 , close3 ) ; return maxLen ; } var s = " " ; document . write ( maxBalancedStr ( s ) ) ;
Bitwise OR of N binary strings | Function to return the bitwise OR of all the binary strings ; Get max size and reverse each string Since we have to perform OR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform OR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code
function strBitwiseOR ( arr , n ) { var res = " " ; var max_size = - 1000000000 ; for ( var i = 0 ; i < n ; i ++ ) { max_size = Math . max ( max_size , arr [ i ] . length ) ; arr [ i ] = arr [ i ] . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; } for ( var i = 0 ; i < n ; i ++ ) { var s = " " ; for ( var j = 0 ; j < max_size - arr [ i ] . length ; j ++ ) s += ' ' ; arr [ i ] = arr [ i ] + s ; } for ( var i = 0 ; i < max_size ; i ++ ) { var curr_bit = 0 ; for ( var j = 0 ; j < n ; j ++ ) curr_bit = curr_bit | ( arr [ j ] [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ; res += String . fromCharCode ( curr_bit + ' ' . charCodeAt ( 0 ) ) ; } res = res . split ( ' ' ) . reverse ( ) . join ( ' ' ) ; return res ; } var arr = [ " " , " " , " " ] ; var n = arr . length ; document . write ( strBitwiseOR ( arr , n ) ) ;
Minimum cost to make two strings same | Function to return the minimum cost to make the configuration of both the strings same ; Iterate and find the cost ; Find the minimum cost ; Driver Code
function findCost ( s1 , s2 , a , b , c , d , n ) { var cost = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) continue ; else { if ( ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) || ( s2 [ i ] == ' ' && s1 [ i ] == ' ' ) ) cost += Math . min ( d , Math . min ( a , b + c ) ) ; else if ( ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) || ( s2 [ i ] == ' ' && s1 [ i ] == ' ' ) ) cost += Math . min ( d , Math . min ( b , a + c ) ) ; else if ( ( s1 [ i ] == ' ' && s2 [ i ] == ' ' ) || ( s2 [ i ] == ' ' && s1 [ i ] == ' ' ) ) cost += Math . min ( d , Math . min ( c , a + b ) ) ; } } return cost ; } var s1 = " " ; var s2 = " " ; var a = 2 , b = 3 , c = 4 , d = 10 ; var n = s1 . length ; document . write ( findCost ( s1 , s2 , a , b , c , d , n ) ) ;
Count the number of common divisors of the given strings | Function that returns true if sub - string s [ 0. . . k ] is repeated a number of times to generate string s ; Function to return the count of common divisors ; If the length of the sub - string divides length of both the strings ; If prefixes match in both the strings ; If both the strings can be generated by repeating the current prefix ; Driver code
function check ( s , k ) { for ( var i = 0 ; i < s . length ; i ++ ) if ( s [ i ] != s [ i % k ] ) return false ; return true ; } function countCommonDivisors ( a , b ) { var ct = 0 ; var n = a . length , m = b . length ; for ( var i = 1 ; i <= Math . min ( n , m ) ; i ++ ) { if ( n % i == 0 && m % i == 0 ) if ( a . substring ( 0 , i ) == b . substring ( 0 , i ) ) if ( check ( a , i ) && check ( b , i ) ) ct ++ ; } return ct ; } var a = " " , b = " " H2 : I6 ; document . write ( countCommonDivisors ( a , b ) ) ;