codeparrot/xlcost-text-to-code huggingface dataset:
Number of sub | Function to return the count of required sub - strings ; Left and right counters for characters on both sides of sub - string window ; Left and right pointer on both sides of sub - string window ; Initialize the frequency ; Result and length of string ; Initialize the left pointer ; Initialize the right pointer ; Traverse all the window sub - strings ; Counting the characters on left side of the sub - string window ; Counting the characters on right side of the sub - string window ; Add the possible sub - strings on both sides to result ; Setting the frequency for next sub - string window ; Reset the left and right counters ; Driver code
function countSubString ( s , c , k ) { var leftCount = 0 , rightCount = 0 ; var left = 0 , right = 0 ; var freq = 0 ; var result = 0 , len = s . length ; while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } right = left + 1 ; while ( freq != ( k - 1 ) && ( right - 1 ) < len ) { if ( s [ right ] == c ) freq ++ ; right ++ ; } while ( left < len && ( right - 1 ) < len ) { while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } while ( right < len && s [ right ] != c ) { if ( s [ right ] == c ) freq ++ ; right ++ ; rightCount ++ ; } result = result + ( leftCount + 1 ) * ( rightCount + 1 ) ; freq = k - 1 ; leftCount = 0 ; rightCount = 0 ; left ++ ; right ++ ; } return result ; } var s = " " ; var c = ' ' ; var k = 2 ; document . write ( countSubString ( s , c , k ) + " " ) ;
Maximum length palindrome that can be created with characters in range L and R | Javascript implementation of the approach ; Function to return the length of the longest palindrome that can be formed using the characters in the range [ l , r ] ; 0 - based indexing ; Marks if there is an odd frequency character ; Length of the longest palindrome possible from characters in range ; Traverse for all characters and count their frequencies ; Find the frequency in range 1 - r ; Exclude the frequencies in range 1 - ( l - 1 ) ; If frequency is odd , then add 1 less than the original frequency to make it even ; Else completely add if even ; If any odd frequency character is present then add 1 ; Function to pre - calculate the frequencies of the characters to reduce complexity ; Iterate and increase the count ; Create a prefix type array ; Driver code ; Pre - calculate prefix array ; Perform queries
let N = 4 ; function performQueries ( l , r , prefix ) { l -- ; r -- ; let flag = false ; let count = 0 ; for ( let i = 0 ; i < 26 ; i ++ ) { let cnt = prefix [ r ] [ i ] ; if ( l > 0 ) cnt -= prefix [ l - 1 ] [ i ] ; if ( cnt % 2 == 1 ) { flag = true ; count += cnt - 1 ; } else count += cnt ; } if ( flag ) count += 1 ; return count ; } function preCalculate ( s , prefix ) { let n = s . length ; for ( let i = 0 ; i < n ; i ++ ) { prefix [ i ] [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } for ( let i = 1 ; i < n ; i ++ ) { for ( let j = 0 ; j < 26 ; j ++ ) prefix [ i ] [ j ] += prefix [ i - 1 ] [ j ] ; } } let s = " " ; let prefix = new Array ( N ) ; for ( let i = 0 ; i < 26 ; i ++ ) { prefix [ i ] = new Array ( 26 ) ; for ( let j = 0 ; j < 26 ; j ++ ) { prefix [ i ] [ j ] = 0 ; } } preCalculate ( s , prefix ) ; let queries = [ [ 1 , 4 ] , [ 3 , 4 ] ] ; let q = queries . length ; for ( let i = 0 ; i < q ; i ++ ) { document . write ( performQueries ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , prefix ) + " " ) ; }
Check whether the given string is a valid identifier | Function that returns true if str is a valid identifier ; If first character is invalid ; Traverse the string for the rest of the characters ; String is a valid identifier ; Driver code
function isValid ( str , n ) { if ( ! ( ( str [ 0 ] >= ' ' && str [ 0 ] <= ' ' ) || ( str [ 0 ] >= ' ' && str [ 0 ] <= ' ' ) str [ 0 ] == ' ' ) ) return false ; for ( let i = 1 ; i < str . length ; i ++ ) { if ( ! ( ( str [ i ] >= ' ' && str [ i ] <= ' ' ) || ( str [ i ] >= ' ' && str [ i ] <= ' ' ) || ( str [ i ] >= ' ' && str [ i ] <= ' ' ) str [ i ] == ' ' ) ) return false ; } return true ; } let str = " " ; let n = str . length ; if ( isValid ( str , n ) ) document . write ( " " ) ; else document . write ( " " ) ;
Check if all the 1 's in a binary string are equidistant or not | Function that returns true if all the 1 's in the binary string s are equidistant ; Initialize vector to store the position of 1 's ; Store the positions of 1 's ; Size of the position vector ; If condition isn 't satisfied ; Drivers code
function check ( s , l ) { var pos = [ ] ; for ( var i = 0 ; i < l ; i ++ ) { if ( s [ i ] == ' ' ) pos . push ( i ) ; } var t = pos . length ; for ( var i = 1 ; i < t ; i ++ ) { if ( ( pos [ i ] - pos [ i - 1 ] ) != ( pos [ 1 ] - pos [ 0 ] ) ) return false ; } return true ; } var s = " " ; var l = s . length ; if ( check ( s , l ) ) document . write ( " " ) ; else document . write ( " " ) ;
Capitalize the first and last character of each word in a string | JavaScript program to capitalise the first and last character of each word in a string . ; Create an equivalent string of the given string ; k stores index of first character and i is going to store index of last character . ; Check if the character is a small letter If yes , then Capitalise ; Driver code
function FirstAndLast ( str ) { var ch = str . split ( ' ' ) ; for ( var i = 0 ; i < ch . length ; i ++ ) { var k = i ; while ( i < ch . length && ch [ i ] != ' ' ) i ++ ; ch [ k ] = String . fromCharCode ( ch [ k ] >= ' ' && ch [ k ] <= ' ' ? ( ch [ k ] . charCodeAt ( 0 ) - 32 ) : ch [ k ] . charCodeAt ( 0 ) ) ; ch [ i - 1 ] = String . fromCharCode ( ch [ i - 1 ] >= ' ' && ch [ i - 1 ] <= ' ' ? ( ch [ i - 1 ] . charCodeAt ( 0 ) - 32 ) : ch [ i - 1 ] . charCodeAt ( 0 ) ) ; } return ch . join ( ' ' ) ; } var str = " " ; document . write ( str + " " ) ; document . write ( FirstAndLast ( str ) ) ;
Find the number of players who roll the dice when the dice output sequence is given | Function to return the number of players ; Initialize cnt as 0 ; Iterate in the string ; Check for numbers other than x ; Driver code
function findM ( s , x ) { var cnt = 0 ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) != x ) cnt ++ ; } return cnt ; } var s = " " ; var x = 6 ; document . write ( findM ( s , x ) ) ;
Print the first and last character of each word in a String | Function to print the first and last character of each word . ; If it is the first word of the string then print it . ; If it is the last word of the string then also print it . ; If there is a space print the successor and predecessor to space . ; Driver code
function FirstAndLast ( str ) { for ( var i = 0 ; i < str . length ; i ++ ) { if ( i == 0 ) document . write ( str [ i ] ) ; if ( i == str . length - 1 ) document . write ( str [ i ] ) ; if ( str [ i ] === " " ) { document . write ( str [ i - 1 ] + " " + str [ i + 1 ] ) ; } } } var str = " " ; FirstAndLast ( str ) ;
Find the longest sub | Function to find longest prefix suffix ; To store longest prefix suffix ; Length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; Loop calculates lps [ i ] for i = 1 to n - 1 ; ( pat [ i ] != pat [ len ] ) ; If len = 0 ; Function to find the longest substring which is prefix as well as a sub - string of s [ 1. . . n - 2 ] ; Find longest prefix suffix ; If lps of n - 1 is zero ; At any position lps [ i ] equals to lps [ n - 1 ] ; If answer is not possible ; Driver code ; function call
function compute_lps ( s ) { var n = s . length ; var lps = Array ( n ) ; var len = 0 ; lps [ 0 ] = 0 ; var i = 1 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) len = lps [ len - 1 ] ; else { lps [ i ] = 0 ; i ++ ; } } } return lps ; } function Longestsubstring ( s ) { var lps = compute_lps ( s ) ; var n = s . length ; if ( lps [ n - 1 ] == 0 ) { document . write ( - 1 ) ; return ; } for ( var i = 0 ; i < n - 1 ; i ++ ) { if ( lps [ i ] == lps [ n - 1 ] ) { document . write ( s . substring ( 0 , lps [ i ] ) ) ; return ; } } if ( lps [ lps [ n - 1 ] - 1 ] == 0 ) document . write ( - 1 ) ; else document . write ( s . substr ( 0 , lps [ lps [ n - 1 ] - 1 ] ) ) ; } var s = " " ; Longestsubstring ( s ) ;
Pairs of strings which on concatenating contains each character of " string " | JavaScript implementation of the above approach ; Function to return the bitmask for the string ; Function to return the count of pairs ; bitMask [ i ] will store the count of strings from the array whose bitmask is i ; To store the count of pairs ; MAX - 1 = 63 i . e . 111111 in binary ; arr [ i ] cannot make s pair with itself i . e . ( arr [ i ] , arr [ i ] ) ; Driver code
const MAX = 64 ; function getBitmask ( s ) { var temp = 0 ; for ( var j = 0 ; j < s . length ; j ++ ) { switch ( s [ j ] ) { case " " : temp = temp | 1 ; break ; case " " : temp = temp | 2 ; break ; case " " : temp = temp | 4 ; break ; case " " : temp = temp | 8 ; break ; case " " : temp = temp | 16 ; break ; case " " : temp = temp | 32 ; break ; default : break ; } } return temp ; } function countPairs ( arr , n ) { var bitMask = new Array ( MAX ) . fill ( 0 ) ; for ( var i = 0 ; i < n ; i ++ ) bitMask [ getBitmask ( arr [ i ] . split ( " " ) ) ] ++ ; var cnt = 0 ; for ( var i = 0 ; i < MAX ; i ++ ) { for ( var j = i ; j < MAX ; j ++ ) { if ( ( i j ) === MAX - 1 ) { if ( i === j ) cnt += parseInt ( ( bitMask [ i ] * bitMask [ i ] - 1 ) / 2 ) ; else cnt += bitMask [ i ] * bitMask [ j ] ; } } } return cnt ; } var arr = [ " " , " " , " " ] ; var n = arr . length ; document . write ( countPairs ( arr , n ) ) ;
Find the count of sub | Function to return the count of required occurrence ; To store the count of occurrences ; Check first four characters from ith position ; Variables for counting the required characters ; Check the four contiguous characters which can be reordered to form ' clap ' ; If all four contiguous characters are present then increment cnt variable ; Driver code
function countOcc ( s ) { var cnt = 0 ; for ( var i = 0 ; i < s . length - 3 ; i ++ ) { var c = 0 , l = 0 , a = 0 , p = 0 ; for ( var j = i ; j < i + 4 ; j ++ ) { switch ( s [ j ] ) { case ' ' : c ++ ; break ; case ' ' : l ++ ; break ; case ' ' : a ++ ; break ; case ' ' : p ++ ; break ; } } if ( c == 1 && l == 1 && a == 1 && p == 1 ) cnt ++ ; } return cnt ; } var s = " " ; s = s . toLowerCase ( ) ; document . write ( countOcc ( s ) ) ;
Number of ways in which the substring in range [ L , R ] can be formed using characters out of the range | Function to return the number of ways to form the sub - string ; Initialize a hash - table with 0 ; Iterate in the string and count the frequency of characters that do not lie in the range L and R ; Out of range characters ; Stores the final number of ways ; Iterate for the sub - string in the range L and R ; If exists then multiply the number of ways and decrement the frequency ; If does not exist the sub - string cannot be formed ; Return the answer ; Driver code
function calculateWays ( s , n , l , r ) { var freq = Array ( 26 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { if ( i < l i > r ) freq [ s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ++ ; } var ways = 1 ; for ( i = l ; i <= r ; i ++ ) { if ( freq [ s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] != 0 ) { ways = ways * freq [ s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] ; freq [ s . charCodeAt ( i ) - ' ' . charCodeAt ( 0 ) ] -- ; } else { ways = 0 ; break ; } } return ways ; } var s = " " ; var n = s . length ; var l = 1 , r = 3 ; document . write ( calculateWays ( s , n , l , r ) ) ;
Convert the ASCII value sentence to its equivalent string | Function to print the character sequence for the given ASCII sentence ; Append the current digit ; If num is within the required range ; Convert num to char ; Reset num to 0 ; Driver code
function asciiToSentence ( str , len ) { var num = 0 ; for ( var i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str [ i ] - ' ' ) ; if ( num >= 32 && num <= 122 ) { var ch = String . fromCharCode ( num ) ; document . write ( ch ) ; num = 0 ; } } } var str = " " ; var len = str . length ; asciiToSentence ( str , len ) ;
Distinct state codes that appear in a string as contiguous sub | Function to return the count of distinct state codes ; Insert every sub - string of length 2 in the set ; Return the size of the set ; Driver code
function countDistinctCode ( str ) { var codes = new Set ( ) ; for ( var i = 0 ; i < str . length - 1 ; i ++ ) codes . add ( str . substr ( i , 2 ) ) ; return codes . size ; } var str = " " ; document . write ( countDistinctCode ( str ) )
Count of buttons pressed in a keypad mobile | Array to store how many times a button has to be pressed for typing a particular character ; Function to return the count of buttons pressed to type the given string ; Count the key presses ; Return the required count ; Driver code
var arr = [ 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 1 , 2 , 3 , 4 ] function countKeyPressed ( str , len ) { var count = 0 ; for ( var i = 0 ; i < len ; i ++ ) count = count + arr [ str . charCodeAt ( i ) - 97 ] ; return count ; } var str = " " ; var len = str . length ; document . write ( countKeyPressed ( str , len ) ) ;
First string from the given array whose reverse is also present in the same array | Function that returns true if s1 is equal to reverse of s2 ; If both the strings differ in length ; In case of any character mismatch ; Function to return the first word whose reverse is also present in the array ; Check every string ; Pair with every other string appearing after the current string ; If first string is equal to the reverse of the second string ; No such string exists ; Driver code
function isReverseEqual ( s1 , s2 ) { if ( s1 . length != s2 . length ) return false ; let len = s1 . length ; for ( let i = 0 ; i < len ; i ++ ) if ( s1 [ i ] != s2 [ len - i - 1 ] ) return false ; return true ; } function getWord ( str , n ) { for ( let i = 0 ; i < n - 1 ; i ++ ) for ( let j = i + 1 ; j < n ; j ++ ) if ( isReverseEqual ( str [ i ] , str [ j ] ) ) return str [ i ] ; return " " ; } let str = [ " " , " " , " " ] ; document . write ( getWord ( str , 3 ) ) ;
Check if the given string is K | Function that return true if sub - string of length k starting at index i is also a prefix of the string ; k length sub - string cannot start at index i ; Character mismatch between the prefix and the sub - string starting at index i ; Function that returns true if str is K - periodic ; Check whether all the sub - strings str [ 0 , k - 1 ] , str [ k , 2 k - 1 ] ... are equal to the k length prefix of the string ; Driver code
function isPrefix ( str , len , i , k ) { if ( i + k > len ) return false ; for ( let j = 0 ; j < k ; j ++ ) { if ( str [ i ] != str [ j ] ) return false ; i ++ ; } return true ; } function isKPeriodic ( str , len , k ) { for ( let i = k ; i < len ; i += k ) if ( ! isPrefix ( str , len , i , k ) ) return false ; return true ; } let str = " " ; let len = str . length ; let k = 5 ; if ( isKPeriodic ( str , len , k ) ) document . write ( " " ) ; else document . write ( " " ) ;
Minimum number of letters needed to make a total of n | Function to return the minimum letters required to make a total of n ; Driver code
function minLettersNeeded ( n ) { if ( n % 26 == 0 ) return parseInt ( n / 26 ) ; else return ( parseInt ( n / 26 ) + 1 ) ; } var n = 52 ; document . write ( minLettersNeeded ( n ) ) ;
Find the first maximum length even word from a string | Function to find maximum length even word ; To store length of current word . ; To store length of maximum length word . ; To store starting index of maximum length word . ; If current character is space then word has ended . Check if it is even length word or not . If yes then compare length with maximum length found so far . ; Set currlen to zero for next word . ; Update length of current word . ; Check length of last word . ; If no even length word is present then return - 1. ; Driver code
function findMaxLenEven ( str ) { var n = str . length ; var i = 0 ; var currlen = 0 ; var maxlen = 0 ; var st = - 1 ; while ( i < n ) { if ( str [ i ] == ' ' ) { if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } currlen = 0 ; } else { currlen ++ ; } i ++ ; } if ( currlen % 2 == 0 ) { if ( maxlen < currlen ) { maxlen = currlen ; st = i - currlen ; } } if ( st == - 1 ) return " " ; return str . substr ( st , maxlen ) ; } var str = " " ; document . write ( findMaxLenEven ( str ) ) ;
Minimum length substring with exactly K distinct characters | Function to find minimum length substring having exactly k distinct character . ; Starting index of sliding window . ; Ending index of sliding window . ; To store count of character . ; To store count of distinct character in current sliding window . ; To store length of current sliding window . ; To store minimum length . ; To store starting index of minimum length substring . ; Increment count of current character If this count is one then a new distinct character is found in sliding window . ; If number of distinct characters is is greater than k , then move starting point of sliding window forward , until count is k . ; Remove characters from the beginning of sliding window having count more than 1 to minimize length . ; Compare length with minimum length and update if required . ; Return minimum length substring . ; Driver code
function findMinLenStr ( str , k ) { var n = str . length ; var st = 0 ; var end = 0 ; var cnt = Array ( 26 ) . fill ( 0 ) ; var distEle = 0 ; var currlen ; var minlen = n ; var startInd = - 1 ; while ( end < n ) { cnt [ str [ end ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; if ( cnt [ str [ end ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 1 ) distEle ++ ; if ( distEle > k ) { while ( st < end && distEle > k ) { if ( cnt [ str [ st ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 1 ) distEle -- ; cnt [ str [ st ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; st ++ ; } } if ( distEle == k ) { while ( st < end && cnt [ str [ st ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] > 1 ) { cnt [ str [ st ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; st ++ ; } currlen = end - st + 1 ; if ( currlen < minlen ) { minlen = currlen ; startInd = st ; } } end ++ ; } return str . substr ( startInd , minlen ) ; } var str = " " ; var k = 4 ; document . write ( findMinLenStr ( str , k ) ) ;
Minimum number of replacements to make the binary string alternating | Set 2 | Function to return the minimum number of characters of the given binary string to be replaced to make the string alternating ; If there is 1 at even index positions ; If there is 0 at odd index positions ; Driver code
function minReplacement ( s , len ) { var ans = 0 ; for ( var i = 0 ; i < len ; i ++ ) { if ( i % 2 == 0 && s [ i ] == ' ' ) ans ++ ; if ( i % 2 == 1 && s [ i ] == ' ' ) ans ++ ; } return Math . min ( ans , len - ans ) ; } var s = " " ; var len = s . length ; document . write ( minReplacement ( s , len ) ) ;
Deletions of "01" or "10" in binary string to make it free from "01" or "10" | Function to return the count of deletions of sub - strings "01" or "10" ; To store the count of 0 s and 1 s ; Driver code
function substrDeletion ( str , len ) { let count0 = 0 , count1 = 0 ; for ( let i = 0 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) count0 ++ ; else count1 ++ ; } return Math . min ( count0 , count1 ) ; } let str = " " ; let len = str . length ; document . write ( substrDeletion ( str , len ) ) ;
Group consecutive characters of same type in a string | Function to return the modified String ; Store original String ; Remove all white spaces ; To store the resultant String ; Traverse the String ; Group upper case characters ; Group numeric characters ; Group arithmetic operators ; Return the resultant String ; Driver code
function groupCharacters ( s , len ) { let temp = " " ; for ( let i = 0 ; i < len ; i ++ ) if ( s [ i ] != ' ' ) temp = temp + s [ i ] ; len = temp . length ; let ans = " " ; let i = 0 ; while ( i < len ) { if ( temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { while ( i < len && temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " " ; } else if ( temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { while ( i < len && temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " " ; } else { while ( i < len && temp [ i ] >= ( ' ' ) && temp [ i ] <= ( ' ' ) ) { ans = ans + temp [ i ] ; i ++ ; } ans = ans + " " ; } } return ans ; } let s = " " ; let len = s . length ; document . write ( groupCharacters ( s . split ( " " ) , len ) ) ;
Find the minimum number of preprocess moves required to make two strings equal | Function to return the minimum number of pre - processing moves required on string A ; Length of the given strings ; To store the required answer ; Run a loop upto n / 2 ; To store frequency of 4 characters ; If size is 4 ; If size is 3 ; If size is 2 ; If n is odd ; Driver code
function Preprocess ( A , B ) { let n = A . length ; let ans = 0 ; for ( let i = 0 ; i < n / 2 ; i ++ ) { let mp = new Map ( ) ; if ( mp . has ( A [ i ] ) ) mp . set ( A [ i ] , mp . get ( A [ i ] ) + 1 ) ; else mp . set ( A [ i ] , 1 ) ; if ( mp . has ( A [ n - i - 1 ] ) ) mp . set ( A [ n - i - 1 ] , mp . get ( A [ n - i - 1 ] ) + 1 ) ; else mp . set ( A [ n - i - 1 ] , 1 ) ; if ( mp . has ( B [ i ] ) ) mp . set ( B [ i ] , mp . get ( B [ i ] ) + 1 ) ; else mp . set ( B [ i ] , 1 ) ; if ( mp . has ( B [ n - i - 1 ] ) ) mp . set ( B [ n - i - 1 ] , mp . get ( B [ n - i - 1 ] ) + 1 ) ; else mp . set ( B [ n - i - 1 ] , 1 ) ; let sz = mp . size ; if ( sz == 4 ) ans += 2 ; else if ( sz == 3 ) ans += 1 + ( A [ i ] == A [ n - i - 1 ] ? 1 : 0 ) ; else if ( sz == 2 ) ans += mp . get ( A [ i ] ) != 2 ? 1 : 0 ; } if ( n % 2 == 1 && A [ Math . floor ( n / 2 ) ] != B [ Math . floor ( n / 2 ) ] ) ans ++ ; return ans ; } let A = " " , B = " " ; document . write ( Preprocess ( A , B ) ) ;
Check if a string can be converted to another string by replacing vowels and consonants | Function to check if the character is vowel or not ; Function that checks if a string can be converted to another string ; Find length of string ; If length is not same ; Iterate for every character ; If both vowel ; Both are consonants ; Driver Code
function isVowel ( c ) { if ( c === " " c === " " c === " " c === " " c === " " ) return true ; return false ; } function checkPossibility ( s1 , s2 ) { var l1 = s1 . length ; var l2 = s2 . length ; if ( l1 !== l2 ) return false ; for ( var i = 0 ; i < l1 ; i ++ ) { if ( isVowel ( s1 [ i ] ) && isVowel ( s2 [ i ] ) ) continue ; else if ( ! isVowel ( s1 [ i ] ) && ! isVowel ( s2 [ i ] ) ) continue ; else return false ; } return true ; } var S1 = " " , S2 = " " ; if ( checkPossibility ( S1 , S2 ) ) document . write ( " " ) ; else document . write ( " " ) ;
Generate a string consisting of characters ' a ' and ' b ' that satisfy the given conditions | Function to generate and print the required string ; More ' b ' , append " bba " ; More ' a ' , append " aab " ; Equal number of ' a ' and ' b ' append " ab " ; Driver code
function generateString ( A , B ) { let rt = " " ; while ( 0 < A 0 < B ) { if ( A < B ) { if ( 0 < B -- ) { rt += ( ' ' ) ; } if ( 0 < B -- ) { rt += ( ' ' ) ; } if ( 0 < A -- ) { rt += ( ' ' ) ; } } else if ( B < A ) { if ( 0 < A -- ) { rt += ( ' ' ) ; } if ( 0 < A -- ) { rt += ( ' ' ) ; } if ( 0 < B -- ) { rt += ( ' ' ) ; } } else { if ( 0 < A -- ) { rt += ( ' ' ) ; } if ( 0 < B -- ) { rt += ( ' ' ) ; } } } document . write ( rt ) ; } let A = 2 , B = 6 ; generateString ( A , B ) ;