codeparrot/xlcost-text-to-code huggingface dataset:
Index of character depending on frequency count in string | C ++ implementation of the approach ; Function to perform the queries ; L [ i ] [ j ] stores the largest i such that ith character appears exactly jth times in str [ 0. . . i ] ; F [ i ] [ j ] stores the smallest i such that ith character appears exactly jth times in str [ 0. . . i ] ; To store the frequency of each of the character of str ; Current character of str ; Update its frequency ; For every lowercase character of the English alphabet ; If it is equal to the character under consideration then update L [ ] [ ] and R [ ] [ ] as it is cnt [ j ] th occurrence of character k ; Only update L [ ] [ ] as k has not been occurred so only index has to be incremented ; Perform the queries ; Type 1 query ; Type 2 query ; Driver code ; Queries ; Perform the queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 26 ; void performQueries ( string str , int q , int type [ ] , char ch [ ] , int freq [ ] ) { int n = str . length ( ) ; int L [ MAX ] [ n ] ; int F [ MAX ] [ n ] ; int cnt [ MAX ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { int k = str [ i ] - ' a ' ; cnt [ k ] ++ ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( k == j ) { L [ j ] [ cnt [ j ] ] = i ; F [ j ] [ cnt [ j ] ] = i ; } else L [ j ] [ cnt [ j ] ] = L [ j ] [ cnt [ j ] ] + 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { if ( type [ i ] == 1 ) { cout << L [ ch [ i ] - ' a ' ] [ freq [ i ] ] ; } else { cout << F [ ch [ i ] - ' a ' ] [ freq [ i ] ] ; } cout << " STRNEWLINE " ; } } int main ( ) { string str = " geeksforgeeks " ; int type [ ] = { 1 , 2 } ; char ch [ ] = { ' e ' , ' k ' } ; int freq [ ] = { 2 , 2 } ; int q = sizeof ( type ) / sizeof ( int ) ; performQueries ( str , q , type , ch , freq ) ; return 0 ; }
Number of index pairs such that s [ i ] and s [ j ] are anagrams | CPP program to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; Function to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; To store count of strings ; Traverse all strings and store in the map ; Sort the string ; Store in the map ; To store the number of pairs ; Traverse through the map ; Count the pairs for each string ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int anagram_pairs ( vector < string > s , int n ) { map < string , int > mp ; for ( int i = 0 ; i < n ; i ++ ) { sort ( s [ i ] . begin ( ) , s [ i ] . end ( ) ) ; mp [ s [ i ] ] ++ ; } int ans = 0 ; for ( auto i = mp . begin ( ) ; i != mp . end ( ) ; i ++ ) { int k = i -> second ; ans += ( k * ( k - 1 ) ) / 2 ; } return ans ; } int main ( ) { vector < string > s = { " aaab " , " aaba " , " baaa " , " cde " , " dec " } ; int n = s . size ( ) ; cout << anagram_pairs ( s , n ) ; return 0 ; }
Insert a Character in a Rotated String | C ++ implementation of the approach ; Function to insert the character ; To store last position where the insertion is done ; To store size of the string ; To store the modified string ; To store characters ; Add first character to the string ; Update the size ; Update the index of last insertion ; Insert all other characters to the string ; Take the character ; Take substring upto ind ; Take modulo value of k with the size of the string ; Check if we need to move to the start of the string ; If we don 't need to move to start of the string ; Take substring from upto temp ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; If we need to move to start of the string ; Take substring which will before the inserted character ; Take substring which will be after the inserted character ; Insert into the string ; Store new inserted position ; Store size of the new string Technically sz + 1 ; Return the required character ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; char Insert ( char arr [ ] , int n , int k ) { int ind = 0 ; int sz = 0 ; string s = " " ; char ch = arr [ 0 ] ; s += ch ; sz = 1 ; ind = 0 ; for ( int i = 1 ; i < n ; i ++ ) { ch = arr [ i ] ; string s1 = s . substr ( 0 , ind + 1 ) ; int temp = k % sz ; int ro = temp - min ( temp , sz - ind - 1 ) ; if ( ro == 0 ) { string s2 = s . substr ( ind + 1 , temp ) ; string s3 = s . substr ( ind + temp + 1 , sz - ind - temp - 1 ) ; s = s1 + s2 + ch + s3 ; ind = s1 . size ( ) + s2 . size ( ) ; sz = s . size ( ) ; } else { string s2 = s . substr ( 0 , ro ) ; string s3 = s . substr ( ro , sz - ro ) ; s = s2 + ch + s3 ; ind = s2 . size ( ) ; sz = s . size ( ) ; } } if ( ind == 0 ) return s [ sz - 1 ] ; else return s [ ind - 1 ] ; } int main ( ) { char arr [ ] = { '1' , '2' , '3' , '4' , '5' } ; int k = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << Insert ( arr , n , k ) ; return 0 ; }
Rearrange the given string such that all prime multiple indexes have same character | CPP program to rearrange the given string such that all prime multiple indexes have same character ; To store answer ; Function to rearrange the given string such that all prime multiple indexes have the same character . ; Initially assume that we can kept any symbol at any positions . If at any index contains one then it is not counted in our required positions ; To store number of positions required to store elements of same kind ; Start sieve ; For all multiples of i ; map to store frequency of each character ; Store all characters in the vector and sort the vector to find the character with highest frequency ; If most occured character is less than required positions ; In all required positions keep character which occured most times ; Fill all other indexes with remaining characters ; If character frequency becomes zero then go to next character ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100005 NEW_LINE char ans [ N ] ; int sieve [ N ] ; void Rearrange ( string s , int n ) { fill ( sieve + 1 , sieve + n + 1 , 1 ) ; int sz = 0 ; for ( int i = 2 ; i <= n / 2 ; i ++ ) { if ( sieve [ i ] ) { for ( int j = 1 ; i * j <= n ; j ++ ) { if ( sieve [ i * j ] ) sz ++ ; sieve [ i * j ] = 0 ; } } } map < char , int > m ; for ( auto it : s ) m [ it ] ++ ; vector < pair < int , char > > v ; for ( auto it : m ) v . push_back ( { it . second , it . first } ) ; sort ( v . begin ( ) , v . end ( ) ) ; if ( v . back ( ) . first < sz ) { cout << -1 ; return ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( ! sieve [ i ] ) { ans [ i ] = v . back ( ) . second ; } } int idx = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( sieve [ i ] ) { ans [ i ] = v [ idx ] . second ; v [ idx ] . first -- ; if ( v [ idx ] . first == 0 ) idx ++ ; } cout << ans [ i ] ; } } int main ( ) { string str = " aabaaaa " ; int n = str . size ( ) ; Rearrange ( str , n ) ; return 0 ; }
Check loop in array according to given constraints | C ++ program to check if a given array is cyclic or not ; A simple Graph DFS based recursive function to check if there is cycle in graph with vertex v as root of DFS . Refer below article for details . https : www . geeksforgeeks . org / detect - cycle - in - a - graph / ; There is a cycle if an adjacent is visited and present in recursion call stack recur [ ] ; Returns true if arr [ ] has cycle ; Create a graph using given moves in arr [ ] ; Do DFS traversal of graph to detect cycle ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isCycleRec ( int v , vector < int > adj [ ] , vector < bool > & visited , vector < bool > & recur ) { visited [ v ] = true ; recur [ v ] = true ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { if ( visited [ adj [ v ] [ i ] ] == false ) { if ( isCycleRec ( adj [ v ] [ i ] , adj , visited , recur ) ) return true ; } else if ( visited [ adj [ v ] [ i ] ] == true && recur [ adj [ v ] [ i ] ] == true ) return true ; } recur [ v ] = false ; return false ; } bool isCycle ( int arr [ ] , int n ) { vector < int > adj [ n ] ; for ( int i = 0 ; i < n ; i ++ ) if ( i != ( i + arr [ i ] + n ) % n ) adj [ i ] . push_back ( ( i + arr [ i ] + n ) % n ) ; vector < bool > visited ( n , false ) ; vector < bool > recur ( n , false ) ; for ( int i = 0 ; i < n ; i ++ ) if ( visited [ i ] == false ) if ( isCycleRec ( i , adj , visited , recur ) ) return true ; return true ; } int main ( void ) { int arr [ ] = { 2 , -1 , 1 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isCycle ( arr , n ) ) cout << " Yes " << endl ; else cout << " No " << endl ; return 0 ; }
Queries to find the last non | C ++ implementation of the approach ; Maximum distinct characters possible ; To store the frequency of the characters ; Function to pre - calculate the frequency array ; Only the first character has frequency 1 till index 0 ; Starting from the second character of the string ; For every possible character ; Current character under consideration ; If it is equal to the character at the current index ; Function to return the frequency of the given character in the sub - string str [ l ... r ] ; Function to return the last non - repeating character ; Starting from the last character ; Current character ; If frequency of the current character is 1 then return the character ; All the characters of the sub - string are repeating ; Driver code ; Pre - calculate the frequency array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MAX = 256 ; int freq [ 256 ] [ 1000 ] = { 0 } ; void preCalculate ( string str , int n ) { freq [ ( int ) str [ 0 ] ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { char ch = str [ i ] ; for ( int j = 0 ; j < MAX ; j ++ ) { char charToUpdate = ( char ) j ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } int getFrequency ( char ch , int l , int r ) { if ( l == 0 ) return freq [ ( int ) ch ] [ r ] ; else return ( freq [ ( int ) ch ] [ r ] - freq [ ( int ) ch ] [ l - 1 ] ) ; } string getString ( char x ) { string s ( 1 , x ) ; return s ; } string lastNonRepeating ( string str , int n , int l , int r ) { for ( int i = r ; i >= l ; i -- ) { char ch = str [ i ] ; if ( getFrequency ( ch , l , r ) == 1 ) return getString ( ch ) ; } return " - 1" ; } int main ( ) { string str = " GeeksForGeeks " ; int n = str . length ( ) ; int queries [ 3 ] [ 2 ] = { { 2 , 9 } , { 2 , 3 } , { 0 , 12 } } ; int q = 3 ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) { cout << ( lastNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) << endl ; ; } }
Queries to answer the X | C ++ implementation of the approach ; Function to pre - process the sub - strings in sorted order ; Generate all substrings ; Iterate to find all sub - strings ; Store the sub - string in the vector ; Sort the substrings lexicographically ; Driver code ; To store all the sub - strings ; Perform queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; void pre_process ( vector < string > & substrings , string s ) { int n = s . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string dup = " " ; for ( int j = i ; j < n ; j ++ ) { dup += s [ j ] ; substrings . push_back ( dup ) ; } } sort ( substrings . begin ( ) , substrings . end ( ) ) ; } int main ( ) { string s = " geek " ; vector < string > substrings ; pre_process ( substrings , s ) ; int queries [ ] = { 1 , 5 , 10 } ; int q = sizeof ( queries ) / sizeof ( queries [ 0 ] ) ; for ( int i = 0 ; i < q ; i ++ ) cout << substrings [ queries [ i ] - 1 ] << endl ; return 0 ; }
Print the final string when minimum value strings get concatenated in every operation | C ++ implementation of the approach ; Class that represents a pair ; Function that prints the final string ; Add all the strings and their corresponding values to the priority queue ; Take those two strings from the priority queue whose corresponding integer values are smaller and add them up as well as their values and add them back to the priority queue while there are more than a single element in the queue ; Get the minimum valued string ; Get the second minimum valued string ; Updated integer value ; Create new entry for the queue ; Add to the queue ; Print the only remaining string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Priority { string s ; int count ; } ; struct Compare { int operator() ( Priority a , Priority b ) { if ( a . count > b . count ) return 1 ; else if ( a . count < b . count ) return -1 ; return 0 ; } } ; static void FindString ( string str [ ] , int num [ ] , int n ) { priority_queue < int , vector < Priority > , Compare > p ; for ( int i = 0 ; i < n ; i ++ ) { Priority x ; x . s = str [ i ] ; x . count = num [ i ] ; p . push ( x ) ; } while ( p . size ( ) > 1 ) { Priority x = p . top ( ) ; p . pop ( ) ; Priority y = p . top ( ) ; p . pop ( ) ; int temp = x . count + y . count ; string sb = x . s + y . s ; Priority z ; z . count = temp ; z . s = sb ; p . push ( z ) ; } Priority z = p . top ( ) ; p . pop ( ) ; cout << z . s << endl ; } int main ( ) { string str [ ] = { " Geeks " , " For " , " Geeks " } ; int num [ ] = { 2 , 3 , 7 } ; int n = sizeof ( num ) / sizeof ( int ) ; FindString ( str , num , n ) ; }
Minimum cost to modify a string | C ++ implementation of the approach ; Function to return the minimum cost ; Initialize result ; To store the frequency of characters of the string ; Initialize array with 0 ; Update the frequencies of the characters of the string ; Loop to check all windows from a - z where window size is K ; Starting index of window ; Ending index of window ; Check if the string contains character ; Check if the character is on left side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Check if the character is on right side of window find the cost of modification for character add value to count calculate nearest distance of modification ; Find the minimum of all costs for modifying the string ; Loop to check all windows Here window contains characters before z and after z of window size K ; Starting index of window ; Ending index of window ; Check if the string contains character ; If characters are outside window find the cost for modifying character add value to count ; Find the minimum of all costs for modifying the string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( string str , int K ) { int n = str . length ( ) ; int res = 999999999 , count = 0 , a , b ; int cnt [ 27 ] ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ str [ i ] - ' a ' + 1 ] ++ ; for ( int i = 1 ; i < ( 26 - K + 1 ) ; i ++ ) { a = i ; b = i + K ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= a && j >= b ) count = count + ( min ( j - b , 25 - j + a + 1 ) ) * cnt [ j ] ; else if ( j <= a && j <= b ) count = count + ( min ( a - j , 25 + j - b + 1 ) ) * cnt [ j ] ; } } res = min ( res , count ) ; } for ( int i = 26 - K + 1 ; i <= 26 ; i ++ ) { a = i ; b = ( i + K ) % 26 ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= b and j <= a ) count = count + ( min ( j - b , a - j ) ) * cnt [ j ] ; } } res = min ( res , count ) ; } return res ; } int main ( ) { string str = " abcdefghi " ; int K = 2 ; cout << minCost ( str , K ) ; return 0 ; }
Generate all binary strings of length n with sub | C ++ implementation of the approach ; Utility function to print the given binary string ; This function will be called recursively to generate the next bit for given binary string according to its current state ; Base - case : if the generated binary string meets the required length and the pattern "01" appears twice ; nextbit needs to be 0 because each time we call the function recursively , we call 2 times for 2 cases : next bit is 0 or 1 The is to assure that the binary string is printed one time only ; Generate the next bit for str and call recursive ; Assign first bit ; The next generated bit will wither be 0 or 1 ; If pattern "01" occurrence is < 2 ; Set next bit ; If pattern "01" appears then increase the occurrence of pattern ; Else pattern "01" occurrence equals 2 ; If previous bit is 0 then next bit cannot be 1 ; Otherwise ; Driver code ; Length of the resulting strings must be at least 4 ; Generate all binary strings of length n with sub - string "01" appearing twice
#include <iostream> NEW_LINE #include <stdlib.h> NEW_LINE using namespace std ; void printBinStr ( int * str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { cout << str [ i ] ; } cout << endl ; } void generateBinStr ( int * str , int len , int currlen , int occur , int nextbit ) { if ( currlen == len ) { if ( occur == 2 && nextbit == 0 ) printBinStr ( str , len ) ; return ; } if ( currlen == 0 ) { str [ 0 ] = nextbit ; generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; } else { if ( occur < 2 ) { str [ currlen ] = nextbit ; if ( str [ currlen - 1 ] == 0 && nextbit == 1 ) { occur += 1 ; } generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; } else { if ( str [ currlen - 1 ] == 0 && nextbit == 1 ) { return ; } else { str [ currlen ] = nextbit ; generateBinStr ( str , len , currlen + 1 , occur , 0 ) ; generateBinStr ( str , len , currlen + 1 , occur , 1 ) ; } } } } int main ( ) { int n = 5 ; if ( n < 4 ) cout << -1 ; else { int * str = new int [ n ] ; generateBinStr ( str , n , 0 , 0 , 0 ) ; generateBinStr ( str , n , 0 , 0 , 1 ) ; } return 0 ; }
Print last character of each word in a string | CPP implementation of the approach ; Function to print the last character of each word in the given string ; Now , last word is also followed by a space ; If current character is a space ; Then previous character must be the last character of some word ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printLastChar ( string str ) { str = str + " ▁ " ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' ▁ ' ) cout << str [ i - 1 ] << " ▁ " ; } } int main ( ) { string str = " Geeks ▁ for ▁ Geeks " ; printLastChar ( str ) ; }
Maximum length of balanced string after swapping and removal of characters | C ++ implementation of the approach ; Function to return the length of the longest balanced sub - string ; To store the count of parentheses ; Traversing the string ; Check type of parentheses and incrementing count for it ; Sum all pair of balanced parentheses ; Driven code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxBalancedStr ( string s ) { int open1 = 0 , close1 = 0 ; int open2 = 0 , close2 = 0 ; int open3 = 0 , close3 = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { switch ( s [ i ] ) { case ' ( ' : open1 ++ ; break ; case ' ) ' : close1 ++ ; break ; case ' { ' : open2 ++ ; break ; case ' } ' : close2 ++ ; break ; case ' [ ' : open3 ++ ; break ; case ' ] ' : close3 ++ ; break ; } } int maxLen = 2 * min ( open1 , close1 ) + 2 * min ( open2 , close2 ) + 2 * min ( open3 , close3 ) ; return maxLen ; } int main ( ) { string s = " ) ) [ ] ] ( ( " ; cout << maxBalancedStr ( s ) ; return 0 ; }
Bitwise OR of N binary strings | C ++ implementation of the approach ; Function to return the bitwise OR of all the binary strings ; Get max size and reverse each string Since we have to perform OR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform OR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string strBitwiseOR ( string * arr , int n ) { string res ; int max_size = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { max_size = max ( max_size , ( int ) arr [ i ] . size ( ) ) ; reverse ( arr [ i ] . begin ( ) , arr [ i ] . end ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { string s ; for ( int j = 0 ; j < max_size - arr [ i ] . size ( ) ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_size ; i ++ ) { int curr_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) curr_bit = curr_bit | ( arr [ j ] [ i ] - '0' ) ; res += ( curr_bit + '0' ) ; } reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { string arr [ ] = { "10" , "11" , "1000001" } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << strBitwiseOR ( arr , n ) ; return 0 ; }
Minimum cost to make two strings same | C ++ implementation of the approach ; Function to return the minimum cost to make the configuration of both the strings same ; Iterate and find the cost ; Find the minimum cost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findCost ( string s1 , string s2 , int a , int b , int c , int d , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 [ i ] == s2 [ i ] ) continue ; else { if ( ( s1 [ i ] == '1' && s2 [ i ] == '2' ) || ( s2 [ i ] == '1' && s1 [ i ] == '2' ) ) cost += min ( d , min ( a , b + c ) ) ; else if ( ( s1 [ i ] == '2' && s2 [ i ] == '3' ) || ( s2 [ i ] == '2' && s1 [ i ] == '3' ) ) cost += min ( d , min ( b , a + c ) ) ; else if ( ( s1 [ i ] == '1' && s2 [ i ] == '3' ) || ( s2 [ i ] == '1' && s1 [ i ] == '3' ) ) cost += min ( d , min ( c , a + b ) ) ; } } return cost ; } int main ( ) { string s1 = "121" ; string s2 = "223" ; int a = 2 , b = 3 , c = 4 , d = 10 ; int n = s1 . size ( ) ; cout << findCost ( s1 , s2 , a , b , c , d , n ) ; return 0 ; }
Count the number of common divisors of the given strings | C ++ implementation of the approach ; Function that returns true if sub - string s [ 0. . . k ] is repeated a number of times to generate string s ; Function to return the count of common divisors ; If the length of the sub - string divides length of both the strings ; If prefixes match in both the strings ; If both the strings can be generated by repeating the current prefix ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int check ( string s , int k ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s [ i ] != s [ i % k ] ) return false ; return true ; } int countCommonDivisors ( string a , string b ) { int ct = 0 ; int n = a . size ( ) , m = b . size ( ) ; for ( int i = 1 ; i <= min ( n , m ) ; i ++ ) { if ( n % i == 0 && m % i == 0 ) if ( a . substr ( 0 , i ) == b . substr ( 0 , i ) ) if ( check ( a , i ) && check ( b , i ) ) ct ++ ; } return ct ; } int main ( ) { string a = " xaxa " , b = " xaxaxaxa " ; cout << countCommonDivisors ( a , b ) ; return 0 ; }
Level order traversal line by line | Set 3 ( Using One Queue ) | C ++ program to print levels line by line ; A Binary Tree Node ; Function to do level order traversal line by line ; Create an empty queue for level order traversal ; to store front element of queue . ; Pushing root node into the queue . ; Pushing delimiter into the queue . ; condition to check occurrence of next level . ; condition to check the occurence of next level ; pushing left child of current node . ; pushing right child of current node . ; Utility function to create a new tree node ; Driver program to test above functions ; Let us create binary tree shown above
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { struct node * left ; int data ; struct node * right ; } ; void levelOrder ( node * root ) { if ( root == NULL ) return ; queue < node * > q ; node * curr ; q . push ( root ) ; q . push ( NULL ) ; while ( q . size ( ) > 1 ) { curr = q . front ( ) ; q . pop ( ) ; if ( curr == NULL ) { q . push ( NULL ) ; cout << " STRNEWLINE " ; } else { if ( curr -> left ) q . push ( curr -> left ) ; if ( curr -> right ) q . push ( curr -> right ) ; cout << curr -> data << " ▁ " ; } } } node * newNode ( int data ) { node * temp = new node ; temp -> data = data ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } int main ( ) { node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> right = newNode ( 6 ) ; levelOrder ( root ) ; return 0 ; }