codeparrot/xlcost-text-to-code huggingface dataset:
Find whether X exists in Y after jumbling X | Javascript implementation of the approach ; Function that returns true if both the arrays have equal values ; Test the equality ; Function that returns true if any permutation of X exists as a substring in Y ; Base case ; To store cumulative frequency ; Finding the frequency of characters in X ; Finding the frequency of characters in Y upto the length of X ; Equality check ; Two pointer approach to generate the entire cumulative frequency ; Remove the first character of the previous window ; Add the last character of the current window ; Equality check ; Driver code
var MAX = 26 ; function areEqual ( a , b ) { for ( var i = 0 ; i < MAX ; i ++ ) if ( a [ i ] != b [ i ] ) return false ; return true ; } function xExistsInY ( x , y ) { if ( x . length > y . length ) return false ; var cnt_x = Array ( MAX ) . fill ( 0 ) ; var cnt = Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < x . length ; i ++ ) cnt_x [ x [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( var i = 0 ; i < x . length ; i ++ ) cnt [ y [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; if ( areEqual ( cnt_x , cnt ) ) return true ; for ( var i = 1 ; i < y . length - x . length + 1 ; i ++ ) { cnt [ y [ i - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; cnt [ y [ i + x . length - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; if ( areEqual ( cnt , cnt_x ) ) return true ; } return false ; } var x = " " ; var y = " " ; if ( xExistsInY ( x , y ) ) document . write ( " " ) ; else document . write ( " " ) ;
Reverse substrings between each pair of parenthesis | Javascript implementation of the approach ; Function to return the modified string ; Push the index of the current opening bracket ; Reverse the substring starting after the last encountered opening bracket till the current character ; To store the modified string ; Driver code
function reverse ( A , l , h ) { if ( l < h ) { let ch = A [ l ] ; A [ l ] = A [ h ] ; A [ h ] = ch ; reverse ( A , l + 1 , h - 1 ) ; } } function reverseParentheses ( str , len ) { let st = [ ] ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) { st . push ( i ) ; } else if ( str [ i ] == ' ' ) { let A = [ ... str ] reverse ( A , st [ st . length - 1 ] + 1 , i ) ; str = [ ... A ] ; st . pop ( ) ; } } let res = " " ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] != ' ' && str [ i ] != ' ' ) { res += ( str [ i ] ) ; } } return res ; } let str = " " ; let len = str . length ; document . write ( reverseParentheses ( str , len ) ) ;
Count of times second string can be formed from the characters of first string | JavaScript implementation of the approach ; Function to update the freq [ ] array to store the frequencies of all the characters of str ; Update the frequency of the characters ; Function to return the maximum count of times patt can be formed using the characters of str ; To store the frequencies of all the characters of str ; To store the frequencies of all the characters of patt ; To store the result ; For every character ; If the current character doesn 't appear in patt ; Update the result ; Driver code
const MAX = 26 ; function updateFreq ( str , freq ) { var len = str . length ; for ( var i = 0 ; i < len ; i ++ ) { freq [ str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ] ++ ; } } function maxCount ( str , patt ) { var strFreq = new Array ( MAX ) . fill ( 0 ) ; updateFreq ( str , strFreq ) ; var pattFreq = new Array ( MAX ) . fill ( 0 ) ; updateFreq ( patt , pattFreq ) ; var ans = 21474836473 ; for ( var i = 0 ; i < MAX ; i ++ ) { if ( pattFreq [ i ] == 0 ) continue ; ans = Math . min ( ans , strFreq [ i ] / pattFreq [ i ] ) ; } return ans ; } var str = " " ; var patt = " " ; document . write ( maxCount ( str , patt ) ) ;
Reduce the number to minimum multiple of 4 after removing the digits | javascript implementation of the approach ; Function to return the minimum number that can be formed after removing the digits which is a multiple of 4 ; For every digit of the number ; Check if the current digit is divisible by 4 ; If any subsequence of two digits is divisible by 4 ; Driver code
var TEN = 10 ; function minNum ( str , len ) { var res = Number . MAX_VALUE ; for ( var i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' str [ i ] == ' ' ) { res = Math . min ( res , str [ i ] - ' ' ) ; } } for ( i = 0 ; i < len - 1 ; i ++ ) { for ( j = i + 1 ; j < len ; j ++ ) { var num = ( str [ i ] - ' ' ) * TEN + ( str [ j ] - ' ' ) ; if ( num % 4 == 0 ) { res = Math . min ( res , num ) ; } } } return ( ( res == Number . MAX_VALUE ) ? - 1 : res ) ; } var str = " " ; var len = str . length ; document . write ( minNum ( str , len ) ) ;
Check if two strings can be made equal by swapping one character among each other | Function that returns true if the string can be made equal after one swap ; A and B are new a and b after we omit the same elements ; Take only the characters which are different in both the strings for every pair of indices ; If the current characters differ ; The strings were already equal ; If the lengths of the strings are two ; If swapping these characters can make the strings equal ; Driver code
function canBeEqual ( a , b , n ) { let A = [ ] ; let B = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { A . push ( a [ i ] ) ; B . push ( b [ i ] ) ; } } if ( A . length == B . length && B . length == 0 ) return true ; if ( A . length == B . length && B . length == 2 ) { if ( A [ 0 ] == A [ 1 ] && B [ 0 ] == B [ 1 ] ) return true ; } return false ; } let A = " " . split ( " " ) ; let B = " " . split ( " " ) ; if ( canBeEqual ( A , B , A . length ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the Mid | Function to find the mid alphabets ; For every character pair ; Get the average of the characters ; Driver code
function findMidAlphabet ( s1 , s2 , n ) { for ( var i = 0 ; i < n ; i ++ ) { var mid = ( s1 [ i ] . charCodeAt ( 0 ) + s2 [ i ] . charCodeAt ( 0 ) ) / 2 ; document . write ( String . fromCharCode ( mid ) ) ; } } var s1 = " " ; var s2 = " " ; var n = s1 . length ; findMidAlphabet ( s1 , s2 , n ) ;
Queries to find the count of vowels in the substrings of the given string | Javascript implementation of the approach ; Function that returns true if ch is a vowel ; pre [ i ] will store the count of vowels in the substring str [ 0. . . i ] ; Fill the pre [ ] array ; If current character is a vowel ; If its a consonant ; For every query ; Find the count of vowels for the current query ; Driver code
var N = 2 ; function isVowel ( ch ) { return ( ch == ' ' ch == ' ' ch == ' ' ch == ' ' ch == ' ' ) ; } function performQueries ( str , len , queries , q ) { var pre = Array ( len ) ; if ( isVowel ( str [ 0 ] ) ) pre [ 0 ] = 1 ; else pre [ 0 ] = 0 ; for ( var i = 1 ; i < len ; i ++ ) { if ( isVowel ( str [ i ] ) ) pre [ i ] = 1 + pre [ i - 1 ] ; else pre [ i ] = pre [ i - 1 ] ; } for ( var i = 0 ; i < q ; i ++ ) { if ( queries [ i ] [ 0 ] == 0 ) { document . write ( pre [ queries [ i ] [ 1 ] ] + " " ) ; } else { document . write ( pre [ queries [ i ] [ 1 ] ] - pre [ queries [ i ] [ 0 ] - 1 ] + " " ) ; } } } var str = " " ; var len = str . length ; var queries = [ [ 1 , 3 ] , [ 2 , 4 ] , [ 1 , 9 ] ] ; var q = queries . length performQueries ( str , len , queries , q ) ;
Check whether the given decoded string is divisible by 6 | Function to return the sum of the digits of n ; Function that return true if the decoded string is divisible by 6 ; To store the sum of the digits ; For each character , get the sum of the digits ; If the sum of digits is not divisible by 3 ; Get the last digit of the number formed ; If the last digit is not divisible by 2 ; Driver code
function sumDigits ( n ) { var sum = 0 ; while ( n > 0 ) { var digit = n % 10 ; sum += digit ; n = parseInt ( n / 10 ) ; } return sum ; } function isDivBySix ( str , n ) { var sum = 0 ; for ( var i = 0 ; i < n ; i ++ ) { sum += ( str [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ) ; } if ( sum % 3 != 0 ) return false ; var lastDigit = ( ( str [ n - 1 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) + 1 ) ) % 10 ; if ( lastDigit % 2 != 0 ) return false ; return true ; } var str = " " ; var n = str . length ; if ( isDivBySix ( str , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check the divisibility of Hexadecimal numbers | Javascript implementation of the approach ; Function that returns true if s is divisible by m ; Map to map characters to real values ; To store the remainder at any stage ; Find the remainder ; Check the value of remainder ; Driver code
var CHARS = " " ; var DIGITS = 16 ; function isDivisible ( s , m ) { var mp = new Map ( ) ; for ( var i = 0 ; i < DIGITS ; i ++ ) { mp . set ( CHARS [ i ] , i ) ; } var r = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) { r = ( r * 16 + mp . get ( s [ i ] ) ) % m ; } if ( ! r ) return true ; return false ; } var s = " " ; var m = 3 ; if ( isDivisible ( s , m ) ) document . write ( " " ) ; else document . write ( " " ) ;
Maximum number of splits of a binary number | Function to return the required count ; If the splitting is not possible ; To store the final ans ; Counting the number of zeros ; Return the final answer ; Driver code
function cntSplits ( s ) { if ( s [ s . length - 1 ] == ' ' ) return - 1 ; var ans = 0 ; for ( var i = 0 ; i < s . length ; i ++ ) ans += ( s [ i ] == ' ' ) ; return ans ; } var s = " " ; document . write ( cntSplits ( s ) ) ;
Modify the string such that every character gets replaced with the next character in the keyboard | JavaScript implementation of the approach ; Function to return the modified string ; Map to store the next character on the keyboard for every possible lowercase character ; Update the string ; Driver code
var CHARS = " " ; var MAX = 26 ; function getString ( string , n ) { var string = string . split ( " " ) ; uMap = [ ] ; for ( let i = 0 ; i < MAX ; i ++ ) { uMap [ CHARS [ i ] ] = CHARS [ ( i + 1 ) % MAX ] ; } for ( let i = 0 ; i < n ; i ++ ) { string [ i ] = uMap [ string [ i ] ] ; } return string . join ( " " ) ; } var string = " " ; var n = string . length ; document . write ( getString ( string , n ) ) ;
Queries to find the count of characters preceding the given location | Javascript implementation of the approach ; returns character at index pos - 1 ; Driver code
function Count ( s , pos ) { let c = s [ pos - 1 ] ; let counter = 0 ; for ( let i = 0 ; i < pos - 1 ; i ++ ) { if ( s [ i ] == c ) counter = counter + 1 ; } return counter ; } let s = " " ; let pos ; let n = s . length ; let query = [ 9 , 3 , 2 ] ; let Q = query . length ; for ( let i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; document . write ( Count ( s , pos ) + " " ) ; }
Count of matchsticks required to represent the given number | stick [ i ] stores the count of sticks required to represent the digit i ; Function to return the count of matchsticks required to represent the given number ; For every digit of the given number ; Add the count of sticks required to represent the current digit ; Driver code
var sticks = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] function countSticks ( str , n ) { var cnt = 0 ; for ( var i = 0 ; i < n ; i ++ ) { cnt += ( sticks [ str [ i ] - ' ' ] ) ; } return cnt ; } var str = " " ; var n = str . length ; document . write ( countSticks ( str , n ) ) ;
Print characters in decreasing order of frequency | Function to print the characters of the given String in decreasing order of their frequencies ; To store the ; Map 's size ; While there are elements in the map ; Finding the maximum value from the map ; Print the character alongwith its frequency ; Delete the maximum value ; Driver code
function printChar ( arr , len ) { let occ = new Map ( ) ; for ( let i = 0 ; i < len ; i ++ ) if ( occ . has ( arr [ i ] ) ) { occ . set ( arr [ i ] , occ . get ( arr [ i ] ) + 1 ) ; } else { occ . set ( arr [ i ] , 1 ) ; } let size = occ . size ; while ( size -- > 0 ) { let currentMax = 0 ; let arg_max = 0 ; for ( let [ key , value ] of occ . entries ( ) ) { if ( value > currentMax || ( value == currentMax && key > arg_max ) ) { arg_max = key ; currentMax = value ; } } document . write ( arg_max + " " + currentMax + " " ) ; occ . delete ( arg_max ) ; } } let str = " " ; let len = str . length ; printChar ( str . split ( " " ) , len ) ;
Modulo of a large Binary String | Function to return the value of ( str % k ) ; pwrTwo [ i ] will store ( ( 2 ^ i ) % k ) ; To store the result ; If current bit is 1 ; Add the current power of 2 ; Driver code
function getMod ( str , n , k ) { var pwrTwo = Array ( n ) ; pwrTwo [ 0 ] = 1 % k ; for ( var i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } var res = 0 ; var i = 0 , j = n - 1 ; while ( i < n ) { if ( str [ j ] == ' ' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } var str = " " ; var n = str . length ; var k = 45 ; document . write ( getMod ( str , n , k ) ) ;
Count number of binary strings such that there is no substring of length greater than or equal to 3 with all 1 's | javascript implementation of the approach ; Function to return the count of all possible valid strings ; Fill 0 's in the dp array ; Base cases ; dp [ i ] [ j ] = number of possible strings such that '1' just appeared consecutively j times upto the ith index ; Taking previously calculated value ; Taking all possible cases that can appear at the Nth position ; Driver code
var MOD = 1000000007 ; function countStrings ( N ) { var i , j ; var dp = Array ( N + 1 ) . fill ( 0 ) . map ( x => Array ( 3 ) . fill ( 0 ) ) ; for ( i = 0 ; i < N + 1 ; i ++ ) { for ( j = 9 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 0 ; for ( i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD ; } var ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD ; return ans ; } var N = 3 ; document . write ( countStrings ( N ) ) ;
Find the maximum possible Binary Number from given string | Function to return maximum number that can be formed from the string ; To store the frequency of ' z ' and ' n ' in the given string ; Number of zeroes ; Number of ones ; To store the required number ; Add all the ones ; Add all the zeroes ; Driver code
function maxNumber ( str , n ) { var freq = Array ( 2 ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { freq [ 0 ] ++ ; } else if ( str [ i ] == ' ' ) { freq [ 1 ] ++ ; } } var num = " " ; for ( var i = 0 ; i < freq [ 1 ] ; i ++ ) num += ' ' ; for ( var i = 0 ; i < freq [ 0 ] ; i ++ ) num += ' ' ; return num ; } var str = " " ; var n = str . length ; document . write ( maxNumber ( str , n ) ) ;
Find the occurrence of the given binary pattern in the binary representation of the array elements | Function to return the binary representation of n ; Array to store binary representation ; Counter for binary array ; Storing remainder in binary array ; To store the binary representation as a string ; Function to return the frequency of pat in the given string txt ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; If pat [ 0. . . M - 1 ] = txt [ i , i + 1 , ... i + M - 1 ] ; Function to find the occurrence of the given pattern in the binary representation of elements of arr [ ] ; For every element of the array ; Find its binary representation ; Print the occurrence of the given pattern in its binary representation ; Driver code
function decToBinary ( n ) { var binaryNum = Array ( 32 ) ; var i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = parseInt ( n / 2 ) ; i ++ ; } var binary = " " ; for ( var j = i - 1 ; j >= 0 ; j -- ) binary += ( binaryNum [ j ] . toString ( ) ) ; return binary ; } function countFreq ( pat , txt ) { var M = pat . length ; var N = txt . length ; var res = 0 ; for ( var i = 0 ; i <= N - M ; i ++ ) { var j ; for ( j = 0 ; j < M ; j ++ ) if ( txt [ i + j ] != pat [ j ] ) break ; if ( j == M ) { res ++ ; j = 0 ; } } return res ; } function findOccurrence ( arr , n , pattern ) { for ( var i = 0 ; i < n ; i ++ ) { var binary = decToBinary ( arr [ i ] ) ; document . write ( countFreq ( pattern , binary ) + " " ) ; } } var arr = [ 5 , 106 , 7 , 8 ] ; var pattern = " " ; var n = arr . length ; findOccurrence ( arr , n , pattern ) ;
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Function that returns true if the sequence can be balanced by changing the position of at most one bracket ; Odd length string can never be balanced ; Add ' ( ' in the beginning and ' ) ' in the end of the string ; If its an opening bracket then append it to the temp string ; If its a closing bracket ; There was an opening bracket to match it with ; No opening bracket to match it with ; Sequence is balanced ; Driver Code
function canBeBalanced ( s , n ) { if ( n % 2 == 1 ) return false ; let k = " " ; k += s + " " ; let d = [ ] ; for ( let i = 0 ; i < k . length ; i ++ ) { if ( k [ i ] == ' ' ) d . push ( " " ) ; else { if ( d . length != 0 ) d . pop ( ) ; else return false ; } } if ( d . length == 0 ) return true ; return false ; } let s = " " ; let n = s . length ; if ( canBeBalanced ( s , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Find the winner of the game | Function to find the winner of the game ; To store the strings for both the players ; If the index is even ; Append the current character to player A 's string ; If the index is odd ; Append the current character to player B 's string ; Sort both the strings to get the lexicographically smallest string possible ; Copmpare both the strings to find the winner of the game ; Driver code
function find_winner ( str , n ) { var str1 = " " , str2 = " " ; for ( var i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { str1 += str [ i ] ; } else { str2 += str [ i ] ; } } str1 = str1 . split ( ' ' ) . sort ( ) ; str2 = str2 . split ( ' ' ) . sort ( ) ; if ( str1 < str2 ) document . write ( " " ) ; else if ( str2 < str1 ) document . write ( " " ) ; else document . write ( " " ) ; } var str = " " ; var n = str . length ; find_winner ( str , n ) ;
Expand the string according to the given conditions | Function to expand and print the given string ; Subtract '0' to convert char to int ; Characters within brackets ; Expanding ; Reset the variables ; Driver code
function expandString ( strin ) { let temp = " " ; let j ; for ( let i = 0 ; i < strin . length ; i ++ ) { if ( strin [ i ] . charCodeAt ( 0 ) >= 0 ) { let num = strin [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( strin [ i + 1 ] == ' ' ) { for ( j = i + 1 ; strin [ j ] != ' ' ; j ++ ) { if ( ( strin [ j ] >= ' ' && strin [ j ] <= ' ' ) || ( strin [ j ] >= ' ' && strin [ j ] <= ' ' ) ) { temp += strin [ j ] ; } } for ( let k = 1 ; k <= num ; k ++ ) { document . write ( temp ) ; } num = 0 ; temp = " " ; if ( j < strin . length ) { i = j ; } } } } } let strin = " " ; expandString ( strin ) ;
Largest substring of str2 which is a prefix of str1 | Function to return the largest substring in str2 which is a prefix of str1 ; To store the index in str2 which matches the prefix in str1 ; While there are characters left in str1 ; If the prefix is not found in str2 ; Remove the last character ; Prefix found ; No substring found in str2 that matches the prefix of str1 ; Driver code
function findPrefix ( str1 , str2 ) { var pos = - 1 ; while ( str1 . length != 0 ) { if ( ! str2 . includes ( str1 ) ) str1 = str1 . substring ( 0 , str1 . length - 1 ) else { pos = str2 . includes ( str1 ) ; break ; } } if ( pos == - 1 ) return " " ; return str1 ; } var str1 = " " ; var str2 = " " ; document . write ( findPrefix ( str1 , str2 ) ) ;
Recursive program to replace all occurrences of pi with 3.14 in a given string | A simple recursive approach to replace all pi in a given function with "3.14" . Firstly function is declared we don 't need any helper function one function is enough ; Base case if s is empty or length of s is 1 return the s ; If the 0 th and 1 st element of s are p and i we have to handle them for rest we have to call recursion it will give the result ; Smalloutput is a variable used to store recursion result ; And we have to add the recursion result with the first part we handled and return the answer ; If 1 st & 2 nd element aren 't "p" & "i", then keep  1st index as it is & call recursion for rest of  the string. ; Driver code ; Function call
function replacePi ( s ) { if ( s . length == 0 s . length == 1 ) return s ; if ( s [ 0 ] == ' ' && s [ 1 ] == ' ' ) { let smallOutput = replacePi ( s . substr ( 2 ) ) ; return " " + smallOutput ; } else { return s [ 0 ] + replacePi ( s . substr ( 1 ) ) ; } } let s = " " ; let result = replacePi ( s ) ; document . write ( result ) ;
Check if the given string is vowel prime | Function that returns true if c is a vowel ; Function that returns true if all the vowels in the given string are only at prime indices ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; For every character of the given string ; If current character is vowel and the index is not prime ; Driver code
function isVowel ( c ) { if ( c == ' ' c == ' ' c == ' ' c == ' ' c == ' ' ) return true ; return false ; } function isVowelPrime ( str , n ) { var prime = Array ( n ) . fill ( true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( var p = 2 ; p * p < n ; p ++ ) { if ( prime [ p ] == true ) { for ( var i = p * p ; i < n ; i += p ) prime [ i ] = false ; } } for ( var i = 0 ; i < n ; i ++ ) { if ( isVowel ( str [ i ] ) && ! prime [ i ] ) return false ; } return true ; } var str = " " ; var n = str . length ; if ( isVowelPrime ( str , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Longest sub string of 0 's in a binary string which is repeated K times | Function to find the longest substring of 0 's ; To store size of the string ; To store the required answer ; Find the longest substring of 0 's ; Run a loop upto s [ i ] is zero ; Check the conditions ; Driver code ; Function call
function longest_substring ( s , k ) { var n = s . length ; if ( k > 1 ) { s += s ; n *= 2 ; } var ans = 0 ; var i = 0 ; while ( i < n ) { var x = 0 ; while ( s [ i ] == ' ' && i < n ) x ++ , i ++ ; ans = Math . max ( ans , x ) ; i ++ ; } if ( k == 1 ans != n ) return ans ; else return ( ans / 2 ) * k ; } var s = " " ; var k = 4 ; document . write ( longest_substring ( s , k ) ) ;
Find the number of occurrences of a character upto preceding position | Function to find the number of occurrences of a character at position P upto p - 1 ; Return the required count ; Driver code ; Function call
function Occurrence ( s , position ) { let count = 0 ; for ( let i = 0 ; i < position - 1 ; i ++ ) if ( s [ i ] == s [ position - 1 ] ) count ++ ; return count ; } let s = " " ; let p = 9 ; document . write ( Occurrence ( s , p ) ) ;
Find the number of occurrences of a character upto preceding position | Function to find the number of occurrences of a character at position P upto p - 1 ; Iterate over the string ; Store the Occurrence of same character ; Increase its frequency ; Return the required count ; Driver code ; Function call
function countOccurrence ( s , position ) { let alpha = new Array ( 26 ) ; for ( let i = 0 ; i < 26 ; i ++ ) { alpha [ i ] = 0 ; } let b = new Array ( s . length ) ; for ( let i = 0 ; i < s . length ; i ++ ) { b [ i ] = alpha [ s [ i ] . charCodeAt ( 0 ) - 97 ] ; alpha [ s [ i ] . charCodeAt ( 0 ) - 97 ] ++ ; } return b [ position - 1 ] ; } let s = " " ; p = 9 ; document . write ( countOccurrence ( s , p ) ) ;