codeparrot/xlcost-text-to-code huggingface dataset:
Find an equal point in a string of brackets | Method to find an equal index ; Store the number of opening brackets at each index ; Store the number of closing brackets at each index ; Check if there is no opening or closing brackets ; Check if there is any index at which both brackets are equal ; Driver Code
function findIndex ( str ) { let len = str . length ; let open = new Array ( len + 1 ) ; let close = new Array ( len + 1 ) ; for ( let i = 0 ; i < len + 1 ; i ++ ) { open [ i ] = 0 ; close [ i ] = 0 ; } let index = - 1 ; open [ 0 ] = 0 ; close [ len ] = 0 ; if ( str [ 0 ] == ' ' ) open [ 1 ] = 1 ; if ( str [ len - 1 ] == ' ' ) close [ len - 1 ] = 1 ; for ( let i = 1 ; i < len ; i ++ ) { if ( str [ i ] == ' ' ) open [ i + 1 ] = open [ i ] + 1 ; else open [ i + 1 ] = open [ i ] ; } for ( let i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] == ' ' ) close [ i ] = close [ i + 1 ] + 1 ; else close [ i ] = close [ i + 1 ] ; } if ( open [ len ] == 0 ) return len ; if ( close [ 0 ] == 0 ) return 0 ; for ( let i = 0 ; i <= len ; i ++ ) if ( open [ i ] == close [ i ] ) index = i ; return index ; } let str = " " ; document . write ( findIndex ( str ) ) ;
Convert decimal fraction to binary number | Function to convert decimal to binary upto k - precision after decimal point ; Fetch the integral part of decimal number ; Fetch the fractional part decimal number ; Conversion of integral part to binary equivalent ; Append 0 in binary ; Reverse string to get original binary equivalent ; Append point before conversion of fractional part ; Conversion of fractional part to binary equivalent ; Find next bit in fraction ; Swap values of left and right ; Driver code
function decimalToBinary ( num , k_prec ) { let binary = " " ; let Integral = parseInt ( num , 10 ) ; let fractional = num - Integral ; while ( Integral > 0 ) { let rem = Integral % 2 ; binary += ( String . fromCharCode ( rem + ' ' . charCodeAt ( ) ) ) ; Integral = parseInt ( Integral / 2 , 10 ) ; } binary = reverse ( binary ) ; binary += ( ' ' ) ; while ( k_prec -- > 0 ) { fractional *= 2 ; let fract_bit = parseInt ( fractional , 10 ) ; if ( fract_bit == 1 ) { fractional -= fract_bit ; binary += String . fromCharCode ( 1 + ' ' . charCodeAt ( ) ) ; } else { binary += String . fromCharCode ( 0 + ' ' . charCodeAt ( ) ) ; } } return binary ; } function reverse ( input ) { let temparray = input . split ( ' ' ) ; let left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { let temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return temparray . join ( " " ) ; } let n = 4.47 ; let k = 3 ; document . write ( decimalToBinary ( n , k ) + " " ) ; n = 6.986 ; k = 5 ; document . write ( decimalToBinary ( n , k ) ) ;
Difference of two large numbers | Returns true if str1 is smaller than str2 , else false . ; Calculate lengths of both string ; Function for finding difference of larger numbers ; Before proceeding further , make sure str1 is not smaller ; Take an empty string for storing result ; Calculate lengths of both string ; Initially take carry zero ; Traverse from end of both strings ; Do school mathematics , compute difference of current digits and carry ; subtract remaining digits of str1 [ ] ; if ( i > 0 sub > 0 ) remove preceding 0 's ; reverse resultant string ; Function call
function isSmaller ( str1 , str2 ) { let n1 = str1 . length , n2 = str2 . length ; if ( n1 < n2 ) return true ; if ( n2 < n1 ) return false ; for ( let i = 0 ; i < n1 ; i ++ ) { if ( str1 [ i ] . charCodeAt ( ) < str2 [ i ] . charCodeAt ( ) ) return true ; else if ( str1 [ i ] . charCodeAt ( ) > str2 [ i ] . charCodeAt ( ) ) return false ; } return false ; } function findDiff ( str1 , str2 ) { if ( isSmaller ( str1 , str2 ) ) { let t = str1 ; str1 = str2 ; str2 = t ; } let str = " " ; let n1 = str1 . length , n2 = str2 . length ; let diff = n1 - n2 ; let carry = 0 ; for ( let i = n2 - 1 ; i >= 0 ; i -- ) { let sub = ( ( str1 [ i + diff ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) - ( str2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) - carry ) ; if ( sub < 0 ) { sub = sub + 10 ; carry = 1 ; } else carry = 0 ; str += sub . toString ( ) ; } for ( let i = n1 - n2 - 1 ; i >= 0 ; i -- ) { if ( str1 [ i ] == ' ' && carry > 0 ) { str += " " ; continue ; } let sub = ( ( str1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ) - carry ) ; str += sub . toString ( ) ; carry = 0 ; } let aa = str . split ( ' ' ) ; aa . reverse ( ) ; return aa . join ( " " ) ; } let str1 = " " ; let str2 = " " ; document . write ( findDiff ( str1 , str2 ) ) ;
Efficiently check if a string has all unique characters without using any additional data structure | Driver code
function unique ( s ) { for ( var i = 0 ; i < s . length ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { return false ; } } return true ; } if ( unique ( " " ) == true ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Check if two strings are k | Optimized Javascript program to check if two strings are k anagram or not . ; Function to check if str1 and str2 are k - anagram or not ; If both strings are not of equal [ i ] then return false ; Store the occurrence of all characters in a hash_array ; Store the occurrence of all characters in a hash_array ; Return true if count is less than or equal to k ; Driver code
let MAX_CHAR = 26 ; function areKAnagrams ( str1 , str2 , k ) { let n = str1 . length ; if ( str2 . length != n ) return false ; let hash_str1 = Array ( MAX_CHAR ) ; hash_str1 . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) hash_str1 [ str1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; let count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( hash_str1 [ str2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] > 0 ) hash_str1 [ str2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] -- ; else count ++ ; if ( count > k ) return false ; } return true ; } let str1 = " " ; let str2 = " " ; let k = 2 ; if ( areKAnagrams ( str1 , str2 , k ) == true ) document . write ( " " ) ; else document . write ( " " ) ;
Nth character in Concatenated Decimal String | Utility method to get dth digit of number N ; Method to return Nth character in concatenated decimal string ; Sum will store character escaped till now ; dist will store numbers escaped till now ; Loop for number lengths ; nine * len will be incremented characters and nine will be incremented numbers ; Restore variables to previous correct state ; Get distance from last one digit less maximum number ; d will store dth digit of current number ; Method will return dth numbered digit of ( dist + diff ) number ; Driver Code
function getDigit ( N , d ) { let str = N . toString ( ) ; return str [ d - 1 ] ; } function getNthChar ( N ) { let sum = 0 , nine = 9 ; let dist = 0 , len ; for ( len = 1 ; ; len ++ ) { sum += nine * len ; dist += nine ; if ( sum >= N ) { sum -= nine * len ; dist -= nine ; N -= sum ; break ; } nine *= 10 ; } let diff = ( Math . ceil ( ( N ) / ( len ) ) ) ; let d = N % len ; if ( d == 0 ) d = len ; return getDigit ( dist + diff , d ) ; } let N = 251 ; document . write ( getNthChar ( N ) ) ;
Minimum characters to be added at front to make string palindrome | function for checking string is palindrome or not ; Driver code ; if string becomes palindrome then break ; erase the last element of the string ; print the number of insertion at front
function ispalindrome ( s ) { let l = s . length ; let j ; for ( let i = 0 , j = l - 1 ; i <= j ; i ++ , j -- ) { if ( s [ i ] != s [ j ] ) return false ; } return true ; } let s = " " ; let cnt = 0 ; let flag = 0 ; while ( s . length > 0 ) { if ( ispalindrome ( s ) ) { flag = 1 ; break ; } else { cnt ++ ; s = s . substring ( 0 , s . length - 1 ) ; } } s . erase ( s . begin ( ) + s . length ( ) - 1 ) ; if ( flag ) document . write ( cnt ) ;
Count characters at same position as in English alphabet | JavaScript program to find number of characters at same position as in English alphabets ; Function to count the number of characters at same position as in English alphabets ; Traverse input string ; Check that index of characters of string is same as of English alphabets by using ASCII values and the fact that all lower case alphabetic characters come together in same order in ASCII table . And same is true for upper case . ; Driver code
function findCount ( str ) { var result = 0 ; function findCount ( $str ) { $result = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) if ( i === str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) || i === str [ i ] . charCodeAt ( 0 ) - " " . charCodeAt ( 0 ) ) result ++ ; return result ; } var str = " " ; document . write ( findCount ( str ) ) ;
Remove a character from a string to make it a palindrome | Utility method to check if substring from low to high is palindrome or not . ; This method returns - 1 if it is not possible to make string a palindrome . It returns - 2 if string is already a palindrome . Otherwise it returns index of character whose removal can make the whole string palindrome . ; Initialize low and right by both the ends of the string ; loop until low and high cross each other ; If both characters are equal then move both pointer towards end ; * If removing str [ low ] makes the * whole string palindrome . We basically * check if substring str [ low + 1. . high ] * is palindrome or not . ; * If removing str [ high ] makes the whole string * palindrome . We basically check if substring * str [ low + 1. . high ] is palindrome or not . ; We reach here when complete string will be palindrome if complete string is palindrome then return mid character ; Driver Code
function isPalindrome ( str , low , high ) { while ( low < high ) { if ( str . charAt ( low ) != str . charAt ( high ) ) return false ; low ++ ; high -- ; } return true ; } function possiblePalinByRemovingOneChar ( str ) { var low = 0 , high = str . length - 1 ; while ( low < high ) { if ( str . charAt ( low ) == str . charAt ( high ) ) { low ++ ; high -- ; } else { if ( isPalindrome ( str , low + 1 , high ) ) return low ; if ( isPalindrome ( str , low , high - 1 ) ) return high ; return - 1 ; } } return - 2 ; } var str = " " ; var idx = possiblePalinByRemovingOneChar ( str ) ; if ( idx == - 1 ) document . write ( " " ) ; else if ( idx == - 2 ) document . write ( " " + " " ) ; else document . write ( " " + " " + idx ) ;
Generate all binary strings from given pattern | Recursive function to generate all binary strings formed by replacing each wildcard character by 0 or 1 ; replace ' ? ' by '0' and recurse ; replace ' ? ' by '1' and recurse ; NOTE : Need to backtrack as string is passed by reference to the function ; Driver Code
function print ( str , index ) { if ( index === str . length ) { document . write ( str . join ( " " ) + " " ) ; return ; } if ( str [ index ] === " " ) { str [ index ] = " " ; print ( str , index + 1 ) ; str [ index ] = " " ; print ( str , index + 1 ) ; str [ index ] = " " ; } else print ( str , index + 1 ) ; } var input = " " ; var str = input . split ( " " ) ; print ( str , 0 ) ;
Find the most frequent digit without using array / string | Simple function to count occurrences of digit d in x ; Initialize count of digit d ; Increment count if current digit is same as d ; Returns the max occurring digit in x ; Handle negative number ; Initialize result which is a digit ; Initialize count of result ; Traverse through all digits ; Count occurrences of current digit ; Update max_count and result if needed ; Driver Code
function countOccurrences ( x , d ) { var count = 0 ; while ( x > 0 ) { if ( x % 10 == d ) count ++ ; x = parseInt ( x / 10 ) ; } return count ; } function maxOccurring ( x ) { if ( x < 0 ) x = - x ; var result = 0 ; var max_count = 1 ; for ( d = 0 ; d <= 9 ; d ++ ) { var count = countOccurrences ( x , d ) ; if ( count >= max_count ) { max_count = count ; result = d ; } } return result ; } var x = 1223355 ; document . write ( " " + maxOccurring ( x ) ) ;
Remove recurring digits in a given number | Removes recurring digits in num [ ] ; let j = 0 ; Index in modified string ; Traverse digits of given number one by one ; Copy the first occurrence of new digit ; Remove repeating occurrences of digit ; Driver code
function removeRecurringDigits ( num ) { let len = num . length ; let s = " " ; for ( let i = 0 ; i < len ; i ++ ) { s += ( num [ i ] ) ; while ( i + 1 < len && num [ i ] == num [ i + 1 ] ) { i ++ ; } } return s ; } let num = " " . split ( " " ) ; document . write ( " " + removeRecurringDigits ( num ) ) ;
Find the maximum subarray XOR in a given array | A simple Javascript program to find max subarray XOR ; Initialize result ; Pick starting points of subarrays ; To store xor of current subarray ; Pick ending points of subarrays starting with i ; Driver code
function maxSubarrayXOR ( arr , n ) { let ans = Number . MIN_VALUE ; for ( let i = 0 ; i < n ; i ++ ) { let curr_xor = 0 ; for ( let j = i ; j < n ; j ++ ) { curr_xor = curr_xor ^ arr [ j ] ; ans = Math . max ( ans , curr_xor ) ; } } return ans ; } let arr = [ 8 , 1 , 2 , 12 ] ; let n = arr . length ; document . write ( " " + maxSubarrayXOR ( arr , n ) ) ;
Print string of odd length in ' X ' format | javascript program to print the given pattern Function to print the given string in respective pattern ; Print characters at corresponding places satisfying the two conditions ; Print blank space at rest of places ; Driver code
function printPattern ( str , len ) { for ( var i = 0 ; i < len ; i ++ ) { for ( var j = 0 ; j < len ; j ++ ) { if ( ( i == j ) || ( i + j == len - 1 ) ) document . write ( str . charAt ( j ) ) ; else document . write ( " " ) ; } document . write ( ' ' ) ; } } var str = " " ; var len = str . length ; printPattern ( str , len ) ;
Find the longest substring with k unique characters in a given string | Javascript program to find the longest substring with k unique characters in a given string ; This function calculates number of unique characters using a associative array count [ ] . Returns true if no . of characters are less than required else returns false . ; Return true if k is greater than or equal to val ; Finds the maximum substring with exactly k unique chars ; Number of unique characters ; Traverse the string , Fills the associative array count [ ] and count number of unique characters ; If there are not enough unique characters , show an error message . ; Otherwise take a window with first element in it . start and end variables . ; Also initialize values for result longest window ; Initialize associative array count [ ] with zero ; put the first character ; Start from the second character and add characters in window according to above explanation ; Add the character ' s [ i ] ' to current window ; If there are more than k unique characters in current window , remove from left side ; Update the max window size if required ; Driver Code
let MAX_CHARS = 26 ; function isValid ( count , k ) { let val = 0 ; for ( let i = 0 ; i < MAX_CHARS ; i ++ ) { if ( count [ i ] > 0 ) { val ++ ; } } return ( k >= val ) ; } function kUniques ( s , k ) { let u = 0 ; let n = s . length ; let count = new Array ( MAX_CHARS ) ; for ( let i = 0 ; i < MAX_CHARS ; i ++ ) { count [ i ] = 0 ; } for ( let i = 0 ; i < n ; i ++ ) { if ( count [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] == 0 ) { u ++ ; } count [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } if ( u < k ) { document . write ( " " ) ; return ; } let curr_start = 0 , curr_end = 0 ; let max_window_size = 1 ; let max_window_start = 0 ; for ( let i = 0 ; i < MAX_CHARS ; i ++ ) { count [ i ] = 0 ; } count [ s [ 0 ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; for ( let i = 1 ; i < n ; i ++ ) { count [ s [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; curr_end ++ ; while ( ! isValid ( count , k ) ) { count [ s [ curr_start ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] -- ; curr_start ++ ; } if ( curr_end - curr_start + 1 > max_window_size ) { max_window_size = curr_end - curr_start + 1 ; max_window_start = curr_start ; } } document . write ( " " + s . substring ( max_window_start , max_window_start + max_window_size + 1 ) + " " + max_window_size ) ; } let s = " " ; let k = 3 ; kUniques ( s , k ) ;
Function to find Number of customers who could not get a computer | JavaScript Program for the above approach ; n is number of computers in cafe . ' seq ' is given sequence of customer entry , exit events ; seen [ i ] = 0 , indicates that customer ' i ' is not in cafe seen [ 1 ] = 1 , indicates that customer ' i ' is in cafe but computer is not assigned yet . seen [ 2 ] = 2 , indicates that customer ' i ' is in cafe and has occupied a computer . ; Initialize result which is number of customers who could not get any computer . ; To keep track of occupied computers ; Traverse the input sequence ; Find index of current character in seen [ 0. . .25 ] ; If First occurrence of ' seq [ i ] ' ; set the current character as seen ; If number of occupied computers is less than n , then assign a computer to new customer ; Set the current character as occupying a computer ; Else this customer cannot get a computer , increment result ; If this is second occurrence of ' seq [ i ] ' ; Decrement occupied only if this customer was using a computer ; Driver program
let MAX_CHAR = 26 ; function runCustomerSimulation ( n , seq ) { let seen = new Array ( MAX_CHAR ) . fill ( 0 ) ; let res = 0 ; let occupied = 0 ; for ( let i = 0 ; i < seq . length ; i ++ ) { let ind = seq [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; if ( seen [ ind ] == 0 ) { seen [ ind ] = 1 ; if ( occupied < n ) { occupied ++ ; seen [ ind ] = 2 ; } else res ++ ; } else { if ( seen [ ind ] == 2 ) { occupied -- ; } seen [ ind ] = 0 ; } } return res ; } document . write ( runCustomerSimulation ( 2 , " " ) + " " ) ; document . write ( runCustomerSimulation ( 3 , " " ) + " " ) ; document . write ( runCustomerSimulation ( 3 , " " ) + " " ) ; document . write ( runCustomerSimulation ( 1 , " " ) + " " ) ; document . write ( runCustomerSimulation ( 1 , " " ) + " " ) ;
Check if characters of a given string can be rearranged to form a palindrome | JavaScript implementation of the above approach ; Bitvector to store the record of which character appear odd and even number of times ; Driver Code
function canFormPalindrome ( a ) { var bitvector = 0 , mask = 0 ; for ( var i = 0 ; i < a . length ; i ++ ) { var x = a . charCodeAt ( i ) - 97 ; mask = 1 << x ; bitvector = bitvector ^ mask ; } return ( ( bitvector & ( bitvector - 1 ) ) == 0 ) ; } if ( canFormPalindrome ( " " ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Print all pairs of anagrams in a given array of strings | JavaScript program to find best meeting point in 2D array ; function to check whether two strings are anagram of each other ; Create two count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; See if there is any non - zero value in count array ; This function prints all anagram pairs in a given array of strings ; Driver Code
let NO_OF_CHARS = 256 function areAnagram ( str1 , str2 ) { let count = [ ] ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) count [ i ] = 0 ; let i = 0 ; while ( i < ( str1 ) . length && i < ( str2 ) . length ) { count [ ord ( str1 [ i ] ) ] += 1 ; count [ ord ( str2 [ i ] ) ] -= 1 ; i += 1 ; } if ( ( str1 ) . length != ( str2 ) . length ) return false ; for ( let i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] ) return false ; return True ; } } function findAllAnagrams ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if areAnagram ( arr [ i ] , arr [ j ] ) document . write ( arr [ i ] + " " + arr [ j ] ) } } } let arr = [ " " , " " , " " , " " , " " ] ; let n = ( arr ) . length ; findAllAnagrams ( arr , n ) ;
Program to print all palindromes in a given range | A function to check if n is palindrome ; Find reverse of n ; If n and rev are same , then n is palindrome ; prints palindrome between min and max ; Driver program to test above function
function isPalindrome ( n ) { var rev = 0 ; for ( var i = n ; Math . trunc ( i ) > 0 ; i /= 10 ) { rev = ( ( rev * 10 ) + ( Math . trunc ( i ) % 10 ) ) ; } return ( n == rev ) ; } function countPal ( min , max ) { for ( var i = min ; i <= max ; i ++ ) { if ( isPalindrome ( i ) ) document . write ( i + " " ) ; } } countPal ( 100 , 2000 ) ;
Length of the longest substring without repeating characters | JavaScript program to find the length of the longest substring without repeating characters ; Result ; Note : Default values in visited are false ; If current character is visited Break the loop ; Else update the result if this window is larger , and mark current character as visited . ; Remove the first character of previous window ; Driver code
function longestUniqueSubsttr ( str ) { var n = str . length ( ) ; var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) { var visited = new [ 256 ] ; for ( var j = i ; j < n ; j ++ ) { if ( visited [ str . charAt ( j ) ] == true ) break ; else { res = Math . max ( res , j - i + 1 ) ; visited [ str . charAt ( j ) ] = true ; } } visited [ str . charAt ( i ) ] = false ; } return res ; } var str = " " ; document . write ( " " + str ) ; var len = longestUniqueSubsttr ( str ) ; document . write ( " " + " " + " " + len ) ;
Find the smallest window in a string containing all characters of another string | Function ; Length of ans ; Starting index of ans ; Creating map ; References of Window ; Traversing the window ; Calculations ; Condition matching ; Sorting ans ; Sliding I Calculation for removing I ; Driver code
function Minimum_Window ( s , t ) { let m = new Array ( 256 ) ; for ( let i = 0 ; i < 256 ; i ++ ) { m [ i ] = 0 ; } let ans = Number . MAX_VALUE ; let start = 0 ; let count = 0 ; for ( let i = 0 ; i < t . length ; i ++ ) { if ( m [ t [ i ] . charCodeAt ( 0 ) ] == 0 ) count ++ ; m [ t [ i ] . charCodeAt ( 0 ) ] ++ ; } let i = 0 ; let j = 0 ; while ( j < s . length ) { m [ s [ j ] . charCodeAt ( 0 ) ] -- ; if ( m [ s [ j ] . charCodeAt ( 0 ) ] == 0 ) count -- ; if ( count == 0 ) { while ( count == 0 ) { if ( ans > j - i + 1 ) { ans = Math . min ( ans , j - i + 1 ) ; start = i ; } m [ s [ i ] . charCodeAt ( 0 ) ] ++ ; if ( m [ s [ i ] . charCodeAt ( 0 ) ] > 0 ) count ++ ; i ++ ; } } j ++ ; } if ( ans != Number . MAX_VALUE ) return ( s ) . join ( " " ) . substring ( start , ( start + ans ) ) ; else return " " ; } let s = " " ; let t = " " ; document . write ( " " + " " ) ; document . write ( Minimum_Window ( s . split ( " " ) , t . split ( " " ) ) ) ;
Run Length Encoding | Javascript program to implement run length encoding ; Count occurrences of current character ; Print character and its count ; Driver code
function printRLE ( str ) { let n = str . length ; for ( let i = 0 ; i < n ; i ++ ) { let count = 1 ; while ( i < n - 1 && str [ i ] == str [ i + 1 ] ) { count ++ ; i ++ ; } document . write ( str [ i ] ) ; document . write ( count ) ; } } let str = " " ; printRLE ( str ) ;
Divide a string in N equal parts | Method to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; length od string is 28 ; Print 4 equal parts of the string
function divideString ( str , n ) { let str_size = str . length ; let part_size ; if ( str_size % n != 0 ) { document . write ( " " + " " ) ; return ; } part_size = parseInt ( str_size / n , 10 ) ; for ( let i = 0 ; i < str_size ; i ++ ) { if ( i % part_size == 0 ) document . write ( " " ) ; document . write ( str [ i ] ) ; } } let str = " " ; divideString ( str , 4 ) ;