codeparrot/xlcost-text-to-code huggingface dataset:
Maximize time by replacing ' _ ' in a given 24 Hour format time | Function to find the maximum time possible by replacing each ' _ ' with any digit ; If the first character is ' _ ' ; If s [ 1 ] is ' _ ' or s [ 1 ] is less than 4 ; Update s [ 0 ] as 2 ; Otherwise , update s [ 0 ] = 1 ; If s [ 1 ] is equal to ' _ ' ; If s [ 0 ] is equal to '2' ; Otherwise ; If S [ 3 ] is equal to ' _ ' ; If s [ 4 ] is equal to ' _ ' ; Print the modified string ; Driver Code
function maximumTime ( str ) { var s = str . split ( " " ) ; if ( s [ 0 ] == ' ' ) { if ( ( s [ 1 ] == ' ' ) || ( s [ 1 ] >= ' ' && s [ 1 ] < ' ' ) ) { s [ 0 ] = ' ' ; } else { s [ 0 ] = ' ' ; } } if ( s [ 1 ] == ' ' ) { if ( s [ 0 ] == ' ' ) { s [ 1 ] = ' ' ; } else { s [ 1 ] = ' ' ; } } if ( s [ 3 ] == ' ' ) { s [ 3 ] = ' ' ; } if ( s [ 4 ] == ' ' ) { s [ 4 ] = ' ' ; } for ( var i = 0 ; i < s . length ; i ++ ) document . write ( s [ i ] ) ; } var S = " " ; maximumTime ( S ) ;
How to create half of the string in uppercase and the other half in lowercase ? |
var string1 = ' ' ; var string1_len = string1 . length ; var half_string = Math . ceil ( string1_len / 2 ) ; var part_a ; var part_b ; part_a = string1 . slice ( 0 , half_string ) ; var new_part_a = part_a . toUpperCase ( ) ; part_b = string1 . slice ( half_string , string1_len ) ; var changed_string = new_part_a + part_b ; console . log ( changed_string ) ;
Nearest power of 2 of frequencies of each digit of a given number | Function to find the nearest power of 2 for all frequencies in the Map freq ; Traverse the Map ; Calculate log of the current array element ; Find the nearest power of 2 for the current frequency ; Function to find nearest power of 2 for frequency of each digit of num ; Length of string ; Stores the frequency of each character in the string ; Traverse the string S ; Function call to generate nearest power of 2 for each frequency ; Driver Code
function nearestPowerOfTwoUtil ( freq ) { freq . forEach ( ( value , key ) => { document . write ( key + " " ) ; var lg = parseInt ( Math . log2 ( value ) ) ; var a = Math . pow ( 2 , lg ) ; var b = Math . pow ( 2 , lg + 1 ) ; if ( ( value - a ) < ( b - value ) ) { document . write ( a + " " ) ; } else { document . write ( b + " " ) ; } } ) ; } function nearestPowerOfTwo ( S ) { var N = S . length ; var freq = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { if ( freq . has ( S [ i ] ) ) { freq . set ( S [ i ] , freq . get ( S [ i ] ) + 1 ) } else { freq . set ( S [ i ] , 1 ) } } nearestPowerOfTwoUtil ( freq ) ; } var N = " " ; nearestPowerOfTwo ( N ) ;
Smallest string divisible by two given strings | Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , print - 1 ; Driver code
function gcd ( a , b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } function lcm ( a , b ) { return ( a / gcd ( a , b ) ) * b ; } function findSmallestString ( s , t ) { let n = s . length , m = t . length ; let l = lcm ( n , m ) ; let s1 = " " , t1 = " " ; for ( let i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( let i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 == ( t1 ) ) { document . write ( s1 + " " ) ; } else { document . write ( - 1 + " " ) ; } } let S = " " , T = " " ; findSmallestString ( S , T ) ;
Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | Function to count minimum number of removal of consecutive 0 s required to make binary consists only of 1 s ; Stores the count of removals ; Traverse the S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code
function minRemovals ( str , N ) { let ans = 0 ; let X = false ; for ( i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' ' ) { ans ++ ; while ( str [ i ] == ' ' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str [ 0 ] == ' ' && str [ N - 1 ] == ' ' ) { ans -- ; } return ans ; } let S = " " ; let N = S . length ; document . write ( minRemovals ( S , N ) ) ;
Minimum removals required to place all 0 s before 1 s in a Binary String | Function to count minimum removals required to arrange all 0 s before 1 s ; Count the occurences of 0 in s ; Size of the string ; Stores the minimum number of removals required ; Iterate over each of the characters in the string s ; If the i - th character is found to be '0' ; Store the minimum of res and right_0 + left_1 in res ; Return the final result ; Driver Code
function minimumDeletions ( s ) { var right_0 = 0 ; var i ; for ( i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == ' ' ) right_0 ++ ; } var left_1 = 0 ; var n = s . length ; var res = 2147483647 ; for ( i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ' ) { right_0 -= 1 ; } else { left_1 += 1 ; } res = Math . min ( res , right_0 + left_1 ) ; } return res ; } var s = " " ; var count = minimumDeletions ( s ) ; document . write ( count ) ;
Minimum substring flips required to convert a Binary String to another | Function to count the minimum number of reversals required to make the given binary strings s1 and s2 same ; Stores the minimum count of reversal of substrings required ; If the length of the strings are not the same then return - 1 ; Iterate over each character ; If s1 [ i ] is not equal to s2 [ i ] ; Iterate until s1 [ i ] != s2 [ i ] ; Increment answer by 1 ; Return the resultant count of reversal of substring required ; Driver Code ; Function Call
function canMakeSame ( s1 , s2 ) { var ans = 0 ; if ( s1 . length != s2 . length ) { return - 1 ; } var N = s1 . length ; for ( var i = 0 ; i < N ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { while ( i < s1 . length && s1 [ i ] != s2 [ i ] ) { i ++ ; } ans ++ ; } } return ans ; } var S1 = " " ; var S2 = " " ; document . write ( canMakeSame ( S1 , S2 ) ) ;
Count points which are revisited while following the path specified by a given string | Function to find the number of times already visited position is revisited after starting traversal from { X , Y } ; Stores the x and y temporarily ; Stores the number of times an already visited position is revisited ; Initialize hashset ; Insert the starting coordinates ; Traverse over the string ; Update the coordinates according to the current directions ; If the new { X , Y } has been visited before , then increment the count by 1 ; Otherwise ; Insert new { x , y } ; Driver Code
function count ( S , X , Y ) { let N = S . length ; let temp_x = 0 , temp_y = 0 ; let count = 0 ; let s = new Set ( ) ; s . add ( ( X + " " + Y ) ) ; for ( let i = 0 ; i < N ; i ++ ) { temp_x = X ; temp_y = Y ; if ( S [ i ] == ' ' ) { X ++ ; } else if ( S [ i ] == ' ' ) { X -- ; } else if ( S [ i ] == ' ' ) { Y ++ ; } else { Y -- ; } if ( s . has ( ( temp_x + X ) + " " + ( temp_y + Y ) ) ) { count ++ ; } else { s . add ( ( temp_x + X ) + " " + ( temp_y + Y ) ) ; } } return count ; } let S = " " ; let X = 0 , Y = 0 ; document . write ( count ( S , X , Y ) ) ;
Sum of frequencies of characters of a string present in another string | Function to find sum of frequencies of characters of S1 present in S2 ; Insert all characters of string S1 in the set ; Traverse the string S2 ; Check if X is present in bset or not ; Increment count by 1 ; Finally , print the count ; Given strings
function countTotalFrequencies ( S1 , S2 ) { var bset = new Set ( ) ; for ( var i = 0 ; i < S1 . length ; i ++ ) { bset . add ( S1 [ i ] ) ; } var count = 0 ; for ( var i = 0 ; i < S2 . length ; i ++ ) { if ( bset . has ( S2 [ i ] ) ) count += 1 ; } document . write ( count ) ; } var S1 = " " ; var S2 = " " ; countTotalFrequencies ( S1 , S2 ) ;
Check if characters of a string can be made non | Function to check if it 's possible to make the String s non decreasing ; Stores length of String ; Stores the last character that is not ' ? ' ; Traverse the String S ; If current character of the String is ' ? ' ; If s [ i ] is not ' ? ' and is less than C ; Otherwise ; Update C ; Return 1 ; Driver Code
function nonDecreasing ( s ) { let size = s . length ; var c = ' ' ; for ( let i = 0 ; i < size ; i ++ ) { if ( s [ i ] == ' ' ) { continue ; } else if ( ( s [ i ] != ' ' ) && ( s [ i ] < c ) ) { return 0 ; } else { c = s [ i ] ; } } return 1 ; } let S = " " ; if ( nonDecreasing ( S . split ( ' ' ) ) ) document . write ( " " + " " ) ; else document . write ( " " + " " ) ;
Make a given Binary String non | Function to return the length of smallest subsequence required to be removed to make the given string non - decreasing ; Length of the string ; Count of zeros and ones ; Traverse the string ; Count minimum removals to obtain strings of the form "00000 . . . . " or "11111 . . . " ; Increment count ; Remove 1 s and remaining 0 s ; Driver Code
function min_length ( str ) { var n = str . length ; var total_zeros = 0 ; var total_ones = 0 ; for ( var i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) total_zeros ++ ; else total_ones ++ ; } var ans = Math . min ( total_zeros , total_ones ) ; var cur_zeros = 0 , cur_ones = 0 ; for ( var i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' ' ) cur_zeros ++ ; else cur_ones ++ ; ans = Math . min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } document . write ( ans ) ; } var str = " " ; min_length ( str ) ;
Check if substrings from three given strings can be concatenated to form a palindrome | Function to check if subStrings from three given Strings can be concatenated to form a palindrome ; Mask for S1 and S2 ; Set ( i - ' a ' ) th bit in maskA ; Set ( i - ' a ' ) th bit in maskC ; If the bitwise AND is > 0 ; Driver Code
function make_palindrome ( S1 , S2 , S3 ) { let maskA = 0 , maskC = 0 ; for ( let i = 0 ; i < S1 . length ; i ++ ) maskA |= ( 1 << ( S1 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; for ( let i = 0 ; i < S3 . length ; i ++ ) maskC |= ( 1 << ( S3 [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ) ) ; if ( ( maskA & maskC ) > 0 ) return " " ; return " " ; } let S1 = " " , S2 = " " , S3 = " " ; document . write ( make_palindrome ( S1 , S2 , S3 ) ) ;
Length of longest prefix anagram which are common in given two strings | Javascript program for the above approach ; Function to check if two arrays are identical or not ; Iterate over the range [ 0 , SIZE ] ; If frequency any character is not same in both the strings ; Otherwise ; Function to find the maximum length of the required string ; Store the count of characters in string str1 ; Store the count of characters in string str2 ; Stores the maximum length ; Minimum length of str1 and str2 ; Increment the count of characters of str1 [ i ] in freq1 [ ] by one ; Increment the count of characters of str2 [ i ] in freq2 [ ] by one ; Checks if prefixes are anagram or not ; Finally print the ans ; Driver code ; Function Call
let SIZE = 26 ; function longHelper ( freq1 , freq2 ) { for ( let i = 0 ; i < SIZE ; ++ i ) { if ( freq1 [ i ] != freq2 [ i ] ) { return false ; } } return true ; } function longCommomPrefixAnagram ( s1 , s2 , n1 , n2 ) { let freq1 = new Array ( 26 ) ; freq1 . fill ( 0 ) ; let freq2 = new Array ( 26 ) ; freq2 . fill ( 0 ) ; let ans = 0 ; let mini_len = Math . min ( n1 , n2 ) ; for ( let i = 0 ; i < mini_len ; ++ i ) { freq1 [ s1 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; freq2 [ s2 [ i ] . charCodeAt ( ) - ' ' . charCodeAt ( ) ] ++ ; if ( longHelper ( freq1 , freq2 ) ) { ans = i + 1 ; } } document . write ( ans ) ; } let str1 = " " ; let str2 = " " ; let N = str1 . length ; let M = str2 . length ; longCommomPrefixAnagram ( str1 , str2 , N , M ) ;
Lexicographic rank of a Binary String | Function to find the rank of a string ; Store the length of the string ; Stores its equivalent decimal value ; Traverse the string ; Store the number of strings of length less than N occurring before the given string ; Store the decimal equivalent number of string bin ; Store the rank in answer ; Print the answer ; Driver program
function findRank ( s ) { var N = s . length ; var bin = " " ; for ( var i = 0 ; i < N ; i ++ ) { if ( s [ i ] == ' ' ) bin += " " ; else bin += " " ; } var X = 1 << N ; var res = 0 , val = 1 ; for ( var i = N - 1 ; i >= 0 ; i -- ) { if ( bin [ i ] == ' ' ) res += ( val ) ; val *= 2 ; } var Y = res ; var ans = X + Y - 1 ; document . write ( ans ) ; } var S = " " ; findRank ( S ) ;
Find the GCD of an array made up of numeric strings | Recursive function to return gcd of A and B ; Base case ; Length of A is greater ; Calculate GCD ; Store the GCD of the length of the Strings ; Driver Code ; Function call
function GCD ( lena , lenb ) { if ( lena == 0 ) return lenb ; if ( lenb == 0 ) return lena ; if ( lena == lenb ) return lena ; if ( lena > lenb ) return GCD ( lena - lenb , lenb ) ; return GCD ( lena , lenb - lena ) ; } function StringGCD ( a , b ) { let gcd = GCD ( a . length , b . length ) ; if ( a . substring ( 0 , gcd ) == ( b . substring ( 0 , gcd ) ) ) { let x = Math . floor ( b . length / gcd ) ; let y = Math . floor ( a . length / gcd ) ; let r = " " , s = " " ; while ( x -- > 0 ) s += a ; while ( y -- > 0 ) r += b ; if ( s == ( r ) ) return a . substring ( 0 , gcd ) ; } return " " ; } let a = " " ; let b = " " ; document . write ( StringGCD ( a , b ) ) ;
Calculate score of a string consisting of balanced parentheses | Function to calculate score of parentheses ; Stores index of character of string ; Stores total scores obtained from the string ; Iterate over characters of the string ; If s [ i ] is ' ( ' ; If top element of stack is ' ( ' ; Stores score of inner parentheses ; Calculate score of inner parentheses ; Update count ; Pop from stack ; Insert score of inner parentheses ; Update i ; Calculate score of the string ; Update ans ; Driver Code
function scoreOfParentheses ( S ) { var s = [ ] ; var i = 0 ; var ans = 0 ; while ( i < S . length ) { if ( S [ i ] == ' ' ) s . push ( " " ) ; else { if ( s [ s . length - 1 ] == " " ) { s . pop ( ) ; s . push ( " " ) ; } else { var count = 0 ; while ( s [ s . length - 1 ] != " " ) { count += parseInt ( s [ s . length - 1 ] ) ; s . pop ( ) ; } s . pop ( ) ; s . push ( ( 2 * count ) . toString ( ) ) ; } } i ++ ; } while ( s . length != 0 ) { ans += parseInt ( s [ s . length - 1 ] ) ; s . pop ( ) ; } return ans ; } var S1 = " " ; document . write ( scoreOfParentheses ( S1 ) ) ;
Minimize cost to rearrange substrings to convert a string to a Balanced Bracket Sequence | Function to count minimum number of operations to convert the string to a balanced bracket sequence ; Initialize the integer array ; Traverse the string ; Decrement a [ i ] ; Increment a [ i ] ; Update the sum as current value of arr [ i ] ; If answer exists ; Traverse from i ; Find all substrings with 0 sum ; Print the sum of sizes ; No answer exists ; Driver Code
function countMinMoves ( str ) { var n = str . length ; var a = Array ( n ) . fill ( 0 ) ; var j , ans = 0 , i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ' ) { a [ i ] += sum - 1 ; } else { a [ i ] += sum + 1 ; } sum = a [ i ] ; } if ( sum == 0 ) { i = 1 ; while ( i < n ) { j = i - 1 ; while ( i < n && a [ i ] != 0 ) i ++ ; if ( i < n && a [ i - 1 ] < 0 ) { ans += i - j ; if ( j == 0 ) ans ++ ; } i ++ ; } document . write ( ans + " " ) ; } else document . write ( " " ) ; } var str = " " ; countMinMoves ( str ) ;
Check if a given string can be converted to a Balanced Bracket Sequence | Function to check if the String can be balanced by replacing the ' $ ' with opening or closing brackets ; If String can never be balanced ; Declare 2 stacks to check if all ) can be balanced with ( or $ and vice - versa ; Store the count the occurence of ( , ) and $ ; Traverse the String ; Increment closed bracket count by 1 ; If there are no opening bracket to match it then return false ; Otherwise , pop the character from the stack ; If current character is an opening bracket or $ , push it to the stack ; Increment symbol count by 1 ; Increment open bracket count by 1 ; Traverse the String from end and repeat the same process ; If there are no closing brackets to match it ; Otherwise , pop character from stack ; Store the extra ( or ) which are not balanced yet ; Check if $ is available to balance the extra brackets ; Count ramaining $ after balancing extra ( and ) ; Check if each pair of $ is convertable in ( ) ; Driver Code ; Function Call
function canBeBalanced ( sequence ) { if ( sequence . length % 2 == 1 ) return false ; let stack_ = [ ] ; let stack2_ = [ ] ; let countOpen = 0 , countClosed = 0 ; let countSymbol = 0 ; for ( let i = 0 ; i < sequence . length ; i ++ ) { if ( sequence [ i ] == ' ' ) { countClosed ++ ; if ( stack_ . length == 0 ) { return false ; } else { stack_ . pop ( ) ; } } else { if ( sequence [ i ] == ' ' ) { countSymbol ++ ; } else { countOpen ++ ; } stack_ . push ( sequence [ i ] ) ; } } for ( let i = sequence . length - 1 ; i >= 0 ; i -- ) { if ( sequence [ i ] == ' ' ) { if ( stack2_ . length == 0 ) { return false ; } else { stack2_ . pop ( ) ; } } else { stack2_ . push ( sequence [ i ] ) ; } } let extra = Math . abs ( countClosed - countOpen ) ; if ( countSymbol < extra ) { return false ; } else { countSymbol -= extra ; if ( countSymbol % 2 == 0 ) { return true ; } } return false ; } let S = " " ; if ( canBeBalanced ( S ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Queries to calculate difference between the frequencies of the most and least occurring characters in specified substring | Function to find difference between maximum and minimum frequency of a character in given range ; Stores length of string ; Stores count of queries ; Iterate over the characters of the string ; Stores l - value of a query ; Stores r - value of a query ; Store count of every character laying in range [ l , r ] ; Update frequency of current character ; Stores maximum frequency of characters in given range ; Stores minimum frequency of characters in given range ; Iterate over all possible characters of the given string ; Update mx ; If ( j + ' a ' ) is present ; Difference between max and min ; Given string ; Given queries ; Function Call
function maxDiffFreq ( queries , S ) { let N = S . length ; let Q = queries . length ; for ( let i = 0 ; i < Q ; ++ i ) { let l = queries [ i ] [ 0 ] - 1 ; let r = queries [ i ] [ 1 ] - 1 ; let freq = new Array ( 26 ) . fill ( 0 ) ; for ( let j = l ; j <= r ; j ++ ) { freq [ S [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ++ ; } let mx = 0 ; let mn = 99999999 ; for ( let j = 0 ; j < 26 ; j ++ ) { mx = Math . max ( mx , freq [ j ] ) ; if ( freq [ j ] ) mn = Math . min ( mn , freq [ j ] ) ; } document . write ( mx - mn + " " ) ; } } let S = " " ; let queries = [ [ 2 , 6 ] , [ 1 , 7 ] ] ; maxDiffFreq ( queries , S ) ;
Count strings from given array having all characters appearing in a given string | Function to count the number of strings from an array having all characters appearing in the string S ; Initialize a set to store all distinct characters of string S ; Traverse over string S ; Insert characters into the Set ; Stores the required count ; Traverse the array ; Traverse over string arr [ i ] ; Check if character in arr [ i ] [ j ] is present in the string S or not ; Increment the count if all the characters of arr [ i ] are present in the string S ; Finally , print the count ; Driver code
function countStrings ( S , list ) { let valid = new Set ( ) ; for ( let x of S ) { valid . add ( x ) ; } let cnt = 0 ; for ( let i = 0 ; i < list . length ; i ++ ) { let j = 0 ; for ( j = 0 ; j < list [ i ] . length ; j ++ ) { if ( valid . has ( list [ i ] [ j ] ) ) continue ; else break ; } if ( j == list [ i ] . length ) cnt ++ ; } return cnt ; } let arr = [ " " , " " , " " , " " ] ; let S = " " ; document . write ( countStrings ( S , arr ) + " " ) ;
Minimize a string by removing all occurrences of another string | Function to find the minimum length to which string str can be reduced to by removing all occurrences of string K ; Initialize stack of characters ; Push character into the stack ; If stack size >= K . size ( ) ; Create empty string to store characters of stack ; Traverse the string K in reverse ; If any of the characters differ , it means that K is not present in the stack ; Push the elements back into the stack ; Store the string ; Remove top element ; Size of stack gives the minimized length of str ; Driver Code ; Function Call
function minLength ( str , N , K , M ) { var stackOfChar = [ ] ; for ( var i = 0 ; i < N ; i ++ ) { stackOfChar . push ( str [ i ] ) ; if ( stackOfChar . length >= M ) { var l = " " ; for ( var j = M - 1 ; j >= 0 ; j -- ) { if ( K [ j ] != stackOfChar [ stackOfChar . length - 1 ] ) { var f = 0 ; while ( f != l . length ) { stackOfChar . push ( l [ f ] ) ; f ++ ; } break ; } else { l = stackOfChar [ stackOfChar . length - 1 ] + l ; stackOfChar . pop ( ) ; } } } } return stackOfChar . length ; } var S1 = " " ; var S2 = " " ; var N = S1 . length ; var M = S2 . length ; document . write ( minLength ( S1 , N , S2 , M ) ) ;
Largest palindrome not exceeding N which can be expressed as product of two 3 | Function to find the largest palindrome not exceeding N which can be expressed as the product of two 3 - digit numbers ; Stores all palindromes ; Stores the product ; Check if X is palindrome ; Check n is less than N ; If true , append it in the list ; Print the largest palindrome ; Driver Code ; Function Call
function palindrome_prod ( N ) { var palindrome_list = [ ] ; var i , j ; for ( i = 101 ; i < 1000 ; i ++ ) { for ( j = 121 ; j < 1000 ; j += ( i % 11 == 0 ) ? 1 : 11 ) { var n = i * j ; var x = n . toString ( ) ; var y = x ; y = y . split ( " " ) . reverse ( ) . join ( " " ) ; if ( x == y ) { if ( n < Number ( N ) ) { palindrome_list . push ( i * j ) ; } } } } document . write ( Math . max . apply ( null , palindrome_list ) ) ; } var N = " " ; palindrome_prod ( N ) ;
Check if given strings can be made same by swapping two characters of same or different strings | Function to check if all strings can be made equal by swapping any pair of characters from the same or different strings ; Stores length of string ; Store frequency of each distinct character of the strings ; Traverse the array ; Iterate over the characters ; Update frequency of arr [ i ] . charAt ( j ) ; Traverse the array cntFreq [ ] ; If cntFreq [ i ] is divisible by N or not ; Driver Code ; Function Call
function isEqualStrings ( arr , N ) { let M = arr [ 0 ] . length ; let cntFreq = new Array ( 256 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { cntFreq [ i ] = 0 ; } for ( let i = 0 ; i < N ; i ++ ) { for ( let j = 0 ; j < M ; j ++ ) { cntFreq [ arr [ i ] [ j ] - ' ' ] += 1 ; } } for ( let i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % N != 0 ) { return false ; } } return true ; } let arr = [ " " , " " , " " ] ; let N = arr . length ; if ( isEqualStrings ( arr , N ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }