codeparrot/xlcost-text-to-code huggingface dataset:
How to validate image file extension using Regular Expression | C ++ program to validate the image file extension using Regular Expression ; Function to validate the image file extension . ; Regex to check valid image file extension . ; If the image file extension is empty return false ; Return true if the image file extension matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool imageFile ( string str ) { const regex pattern ( " [ ^ \\ s ] + ( . * ? ) \\ . ( jpg ▁ jpeg ▁ png ▁ gif ▁ JPG ▁ JPEG ▁ PNG ▁ GIF ) $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " abc . png " ; cout << imageFile ( str1 ) << endl ; string str2 = " im . jpg " ; cout << imageFile ( str2 ) << endl ; string str3 = " . gif " ; cout << imageFile ( str3 ) << endl ; string str4 = " abc . mp3" ; cout << imageFile ( str4 ) << endl ; string str5 = " ▁ . jpg " ; cout << imageFile ( str5 ) << endl ; return 0 ; }
How to check Aadhar number is valid or not using Regular Expression | C ++ program to validate the Aadhar number using Regular Expression ; Function to validate the Aadhar number . ; Regex to check valid Aadhar number . ; If the Aadhar number is empty return false ; Return true if the Aadhar number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidAadharNumber ( string str ) { const regex pattern ( " ^ [ 2-9 ] { 1 } [ 0-9 ] { 3 } \\ s [ 0-9 ] { 4 } \\ s [ 0-9 ] { 4 } $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "3675 ▁ 9834 ▁ 6015" ; cout << isValidAadharNumber ( str1 ) << endl ; string str2 = "4675 ▁ 9834 ▁ 6012 ▁ 8" ; cout << isValidAadharNumber ( str2 ) << endl ; string str3 = "0175 ▁ 9834 ▁ 6012" ; cout << isValidAadharNumber ( str3 ) << endl ; string str4 = "3675 ▁ 98AF ▁ 60#2" ; cout << isValidAadharNumber ( str4 ) << endl ; string str5 = "417598346012" ; cout << isValidAadharNumber ( str5 ) << endl ; return 0 ; }
How to validate pin code of India using Regular Expression | C ++ program to validate the pin code of India using Regular Expression . ; Function to validate the pin code of India . ; Regex to check valid pin code of India . ; If the pin code is empty return false ; Return true if the pin code matched the ReGex ; Driver Code . ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isValidPinCode ( string pinCode ) { const regex pattern ( " ^ [ 1-9 ] { 1 } [ 0-9 ] { 2 } \\ s { 0,1 } [ 0-9 ] { 3 } $ " ) ; if ( pinCode . empty ( ) ) { return false ; } if ( regex_match ( pinCode , pattern ) ) { return true ; } else { return false ; } } void print ( bool n ) { if ( n == 0 ) { cout << " False " << endl ; } else { cout << " True " << endl ; } } int main ( ) { string num1 = "132103" ; cout << num1 + " : ▁ " ; print ( isValidPinCode ( num1 ) ) ; string num2 = "201 ▁ 305" ; cout << num2 + " : ▁ " ; print ( isValidPinCode ( num2 ) ) ; string num3 = "014205" ; cout << num3 + " : ▁ " ; print ( isValidPinCode ( num3 ) ) ; string num4 = "1473598" ; cout << num4 + " : ▁ " ; print ( isValidPinCode ( num4 ) ) ; return 0 ; }
How to validate time in 24 | C ++ program to validate time in 24 - hour format using Regular Expression ; Function to validate time in 24 - hour format ; Regex to check valid time in 24 - hour format ; If the time in 24 - hour format is empty return false ; Return true if the time in 24 - hour format matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidTime ( string str ) { const regex pattern ( " ( [01 ] ? [0-9 ] ▁ 2[0-3 ] ) : [ 0-5 ] [ 0-9 ] " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "13:05" ; cout << str1 + " : ▁ " << isValidTime ( str1 ) << endl ; string str2 = "02:15" ; cout << str2 + " : ▁ " << isValidTime ( str2 ) << endl ; string str3 = "24:00" ; cout << str3 + " : ▁ " << isValidTime ( str3 ) << endl ; string str4 = "10:60" ; cout << str4 + " : ▁ " << isValidTime ( str4 ) << endl ; string str5 = "10:15 ▁ PM " ; cout << str5 + " : ▁ " << isValidTime ( str5 ) << endl ; return 0 ; }
How to validate Hexadecimal Color Code using Regular Expression | C ++ program to validate the hexadecimal color code using Regular Expression ; Function to validate the hexadecimal color code . ; Regex to check valid hexadecimal color code . ; If the hexadecimal color code is empty return false ; Return true if the hexadecimal color code matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidHexaCode ( string str ) { const regex pattern ( " ^ # ( [ A - Fa - f0-9 ] { 6 } ▁ [ A - Fa - f0-9 ] { 3 } ) $ " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " # 1AFFa1" ; cout << str1 + " : ▁ " << isValidHexaCode ( str1 ) << endl ; string str2 = " # F00" ; cout << str2 + " : ▁ " << isValidHexaCode ( str2 ) << endl ; string str3 = "123456" ; cout << str3 + " : ▁ " << isValidHexaCode ( str3 ) << endl ; string str4 = " # 123abce " ; cout << str4 + " : ▁ " << isValidHexaCode ( str4 ) << endl ; string str5 = " # afafah " ; cout << str5 + " : ▁ " << isValidHexaCode ( str5 ) << endl ; return 0 ; }
How to validate PAN Card number using Regular Expression | C ++ program to validate the PAN Card number using Regular Expression ; Function to validate the PAN Card number . ; Regex to check valid PAN Card number . ; If the PAN Card number is empty return false ; Return true if the PAN Card number matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 : ; Test Case 5 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidPanCardNo ( string panCardNo ) { const regex pattern ( " [ A - Z ] { 5 } [ 0-9 ] { 4 } [ A - Z ] { 1 } " ) ; if ( panCardNo . empty ( ) ) { return false ; } if ( regex_match ( panCardNo , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = " BNZAA2318J " ; cout << isValidPanCardNo ( str1 ) << endl ; string str2 = "23ZAABN18J " ; cout << isValidPanCardNo ( str2 ) << endl ; string str3 = " BNZAA2318JM " ; cout << isValidPanCardNo ( str3 ) << endl ; string str4 = " BNZAA23184" ; cout << isValidPanCardNo ( str4 ) << endl ; string str5 = " BNZAA ▁ 23184" ; cout << isValidPanCardNo ( str5 ) << endl ; return 0 ; }
How to validate time in 12 | C ++ program to validate time in 12 - hour format using Regular Expression ; Function to validate time in 12 - hour format ; Regex to check valid time in 12 - hour format ; If the time in 12 - hour format is empty return false ; Return true if the time in 12 - hour format matched the ReGex ; Driver Code ; Test Case 1 : ; Test Case 2 : ; Test Case 3 : ; Test Case 4 :
#include <iostream> NEW_LINE #include <regex> NEW_LINE using namespace std ; bool isValidTime ( string str ) { const regex pattern ( " ( ( ( [1-9 ] ) | ( 1[0-2 ] ) ) : ( [ 0-5 ] ) ( [ 0-9 ] ) ( \\ s ) ( A ▁ P ) M ) " ) ; if ( str . empty ( ) ) { return false ; } if ( regex_match ( str , pattern ) ) { return true ; } else { return false ; } } int main ( ) { string str1 = "12:15 ▁ AM " ; cout << isValidTime ( str1 ) << endl ; string str2 = "9:45 ▁ PM " ; cout << isValidTime ( str2 ) << endl ; string str3 = "1:15" ; cout << isValidTime ( str3 ) << endl ; string str4 = "17:30" ; cout << isValidTime ( str4 ) << endl ; return 0 ; }
Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity | C ++ implementation to find the minimum flips required such that alternate subarrays have different parity ; Function to find the minimum flips required in binary array ; Boolean value to indicate odd or even value of 1 's ; Loop to iterate over the subarrays of size K ; curr_index is used to iterate over all the subarrays ; Loop to iterate over the array at the jump of K to consider every parity ; Condition to check if the subarray is at even position ; The value needs to be same as the first subarray ; The value needs to be opposite of the first subarray ; Update the minimum difference ; Condition to check if the 1 s in the subarray is odd ; Driver Code
#include <iostream> NEW_LINE #include <limits.h> NEW_LINE using namespace std ; int count_flips ( int a [ ] , int n , int k ) { bool set = false ; int ans = 0 , min_diff = INT_MAX ; for ( int i = 0 ; i < k ; i ++ ) { int curr_index = i , segment = 0 , count_zero = 0 , count_one = 0 ; while ( curr_index < n ) { if ( segment % 2 == 0 ) { if ( a [ curr_index ] == 1 ) count_zero ++ ; else count_one ++ ; } else { if ( a [ curr_index ] == 0 ) count_zero ++ ; else count_one ++ ; } curr_index = curr_index + k ; segment ++ ; } ans += min ( count_one , count_zero ) ; if ( count_one < count_zero ) set = ! set ; min_diff = min ( min_diff , abs ( count_zero - count_one ) ) ; } if ( set ) return ans ; else return ans + min_diff ; } int main ( ) { int n = 6 , k = 3 ; int a [ ] = { 0 , 0 , 1 , 1 , 0 , 0 } ; cout << count_flips ( a , n , k ) ; }
Program to build a DFA that checks if a string ends with "01" or "10" | CPP Program to DFA that accepts string ending with 01 or 10. ; Various states of DFA machine are defined using functions . ; End position is checked using the string length value . q0 is the starting state . q1 and q2 are intermediate states . q3 and q4 are final states . ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q4 , 1 takes to q2 ; state transitions 0 takes to q1 , 1 takes to q3 ; state transitions 0 takes to q1 , 1 takes to q2 ; Driver Code ; all state transitions are printed . if string is accpetable , YES is printed . else NO is printed
#include <bits/stdc++.h> NEW_LINE using namespace std ; void q1 ( string , int ) ; void q2 ( string , int ) ; void q3 ( string , int ) ; void q4 ( string , int ) ; void q1 ( string s , int i ) { cout << " q1 - > " ; if ( i == s . length ( ) ) { cout << " NO ▁ STRNEWLINE " ; return ; } if ( s [ i ] == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } void q2 ( string s , int i ) { cout << " q2 - > " ; if ( i == s . length ( ) ) { cout << " NO ▁ STRNEWLINE " ; return ; } if ( s [ i ] == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } void q3 ( string s , int i ) { cout << " q3 - > " ; if ( i == s . length ( ) ) { cout << " YES ▁ STRNEWLINE " ; return ; } if ( s [ i ] == '0' ) q4 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } void q4 ( string s , int i ) { cout << " q4 - > " ; if ( i == s . length ( ) ) { cout << " YES ▁ STRNEWLINE " ; return ; } if ( s [ i ] == '0' ) q1 ( s , i + 1 ) ; else q3 ( s , i + 1 ) ; } void q0 ( string s , int i ) { cout << " q0 - > " ; if ( i == s . length ( ) ) { cout << " NO ▁ STRNEWLINE " ; return ; } if ( s [ i ] == '0' ) q1 ( s , i + 1 ) ; else q2 ( s , i + 1 ) ; } int main ( ) { string s = "010101" ; cout << " State ▁ transitions ▁ are ▁ " ; q0 ( s , 0 ) ; }
Check whether two strings contain same characters in same order |  ; if length of the b = 0 then we return true ; if length of a = 0 that means b is not present in a so we return false ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool checkSequence ( string a , string b ) { if ( b . size ( ) == 0 ) return true ; if ( a . size ( ) == 0 ) return false ; if ( a [ 0 ] == b [ 0 ] ) return checkSequence ( a . substr ( 1 ) , b . substr ( 1 ) ) ; else return checkSequence ( a . substr ( 1 ) , b ) ; } int main ( ) { string s1 = " Geeks " , s2 = " Geks " ; if ( checkSequence ( s1 , s2 ) ) cout << " Yes " ; else cout << " No " ; }
String matching with * ( that matches with any ) in any of the two strings | CPP program for string matching with * ; Function to check if the two strings can be matched or not ; if the string don 't have *  then character at that position  must be same. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool doMatch ( string A , string B ) { for ( int i = 0 ; i < A . length ( ) ; i ++ ) if ( A [ i ] != ' * ' && B [ i ] != ' * ' ) if ( A [ i ] != B [ i ] ) return false ; return true ; } int main ( ) { string A = " gee * sforgeeks " ; string B = " geeksforgeeks " ; cout << doMatch ( A , B ) ; return 0 ; }
Find Nth term of the series 0 , 2 , 4 , 8 , 12 , 18. . . | CPP program to find N - th term of the series : 0 , 2 , 4 , 8 , 12 , 18. . . ; Calculate Nth term of series ; Driver Function
#include <iostream> NEW_LINE using namespace std ; int nthTerm ( int N ) { return ( N + N * ( N - 1 ) ) / 2 ; } int main ( ) { int N = 5 ; cout << nthTerm ( N ) ; return 0 ; }
Number of substrings of one string present in other | CPP program to count number of substrings of s1 present in s2 . ; s3 stores all substrings of s1 ; check the presence of s3 in s2 ; Driver code
#include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; int countSubstrs ( string s1 , string s2 ) { int ans = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { string s3 ; for ( int j = i ; j < s1 . length ( ) ; j ++ ) { s3 += s1 [ j ] ; if ( s2 . find ( s3 ) != string :: npos ) ans ++ ; } } return ans ; } int main ( ) { string s1 = " aab " , s2 = " aaaab " ; cout << countSubstrs ( s1 , s2 ) ; return 0 ; }
Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | CPP program to find N - th term of the series : 1 , 4 , 15 , 72 , 420 a ; Function to find factorial of N ; return factorial of N ; calculate Nth term of series ; Driver Function
#include <iostream> NEW_LINE using namespace std ; int factorial ( int N ) { int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fact = fact * i ; return fact ; } int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } int main ( ) { int N = 6 ; cout << nthTerm ( N ) ; return 0 ; }
Sum of nodes at maximum depth of a Binary Tree | Code to find the sum of the nodes which are present at the maximum depth . ; Function to return a new node ; Function to find the sum of the node which are present at the maximum depth . While traversing the nodes compare the level of the node with max_level ( Maximum level till the current node ) . If the current level exceeds the maximum level , update the max_level as current level . If the max level and current level are same , add the root data to current sum . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum = 0 , max_level = INT_MIN ; struct Node { int d ; Node * l ; Node * r ; } ; Node * createNode ( int d ) { Node * node ; node = new Node ; node -> d = d ; node -> l = NULL ; node -> r = NULL ; return node ; } void sumOfNodesAtMaxDepth ( Node * ro , int level ) { if ( ro == NULL ) return ; if ( level > max_level ) { sum = ro -> d ; max_level = level ; } else if ( level == max_level ) { sum = sum + ro -> d ; } sumOfNodesAtMaxDepth ( ro -> l , level + 1 ) ; sumOfNodesAtMaxDepth ( ro -> r , level + 1 ) ; } int main ( ) { Node * root ; root = createNode ( 1 ) ; root -> l = createNode ( 2 ) ; root -> r = createNode ( 3 ) ; root -> l -> l = createNode ( 4 ) ; root -> l -> r = createNode ( 5 ) ; root -> r -> l = createNode ( 6 ) ; root -> r -> r = createNode ( 7 ) ; sumOfNodesAtMaxDepth ( root , 0 ) ; cout << sum ; return 0 ; }
Match Expression where a single special character in pattern can match one or more characters | C ++ program for pattern matching where a single special character can match one more characters ; Returns true if pat matches with text ; i is used as an index in pattern and j as an index in text ; Traverse through pattern ; If current character of pattern is not ' # ' ; If does not match with text ; If matches , increment i and j ; Current character is ' # ' ; At least one character must match with # ; Match characters with # until a matching character is found . ; Matching with # is over , move ahead in pattern ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int regexMatch ( string text , string pat ) { int lenText = text . length ( ) ; int letPat = pat . length ( ) ; int i = 0 , j = 0 ; while ( i < letPat ) { if ( pat [ i ] != ' # ' ) { if ( pat [ i ] != text [ j ] ) return false ; i ++ ; j ++ ; } else { j ++ ; while ( text [ j ] != pat [ i + 1 ] ) j ++ ; i ++ ; } } return ( j == lenText ) ; } int main ( ) { string str = " ABABABA " ; string pat = " A # B # A " ; if ( regexMatch ( str , pat ) ) cout << " yes " ; else cout << " no " ; return 0 ; }
Replace all occurrences of string AB with C without using extra space | Efficient C ++ program to replace all occurrences of " AB " with " C " ; Index in modified string ; Index in original string ; Traverse string ; Replace occurrence of " AB " with " C " ; Increment j by 2 ; add a null character to terminate string ; Driver code
#include <bits/stdc++.h> NEW_LINE void translate ( char * str ) { int len = strlen ( str ) ; if ( len < 2 ) return ; int i = 0 ; int j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == ' A ' && str [ j + 1 ] == ' B ' ) { j = j + 2 ; str [ i ++ ] = ' C ' ; continue ; } str [ i ++ ] = str [ j ++ ] ; } if ( j == len - 1 ) str [ i ++ ] = str [ j ] ; str [ i ] = ' ' ; } int main ( ) { char str [ ] = " helloABworldABGfG " ; translate ( str ) ; printf ( " The ▁ modified ▁ string ▁ is ▁ : STRNEWLINE " ) ; printf ( " % s " , str ) ; }
Search a Word in a 2D Grid of characters | C ++ programs to search a word in a 2D grid ; For searching in all 8 direction ; This function searches in all 8 - direction from point ( row , col ) in grid [ ] [ ] ; If first character of word doesn 't  match with given starting point in grid. ; Search word in all 8 directions starting from ( row , col ) ; Initialize starting point for current direction ; First character is already checked , match remaining characters ; If out of bound break ; If not matched , break ; Moving in particular direction ; If all character matched , then value of k must be equal to length of word ; Searches given word in a given matrix in all 8 directions ; Consider every point as starting point and search given word ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int x [ ] = { -1 , -1 , -1 , 0 , 0 , 1 , 1 , 1 } ; int y [ ] = { -1 , 0 , 1 , -1 , 1 , -1 , 0 , 1 } ; bool search2D ( char * grid , int row , int col , string word , int R , int C ) { if ( * ( grid + row * C + col ) != word [ 0 ] ) return false ; int len = word . length ( ) ; for ( int dir = 0 ; dir < 8 ; dir ++ ) { int k , rd = row + x [ dir ] , cd = col + y [ dir ] ; for ( k = 1 ; k < len ; k ++ ) { if ( rd >= R rd < 0 cd > = C cd < 0 ) break ; if ( * ( grid + rd * C + cd ) != word [ k ] ) break ; rd += x [ dir ] , cd += y [ dir ] ; } if ( k == len ) return true ; } return false ; } void patternSearch ( char * grid , string word , int R , int C ) { for ( int row = 0 ; row < R ; row ++ ) for ( int col = 0 ; col < C ; col ++ ) if ( search2D ( grid , row , col , word , R , C ) ) cout << " pattern ▁ found ▁ at ▁ " << row << " , ▁ " << col << endl ; } int main ( ) { int R = 3 , C = 13 ; char grid [ R ] [ C ] = { " GEEKSFORGEEKS " , " GEEKSQUIZGEEK " , " IDEQAPRACTICE " } ; patternSearch ( ( char * ) grid , " GEEKS " , R , C ) ; cout << endl ; patternSearch ( ( char * ) grid , " EEE " , R , C ) ; return 0 ; }
Boyer Moore Algorithm for Pattern Searching | C ++ Program for Bad Character Heuristic of Boyer Moore String Matching Algorithm ; The preprocessing function for Boyer Moore 's bad character heuristic ; Initialize all occurrences as - 1 ; Fill the actual value of last occurrence of a character ; A pattern searching function that uses Bad Character Heuristic of Boyer Moore Algorithm ; Fill the bad character array by calling the preprocessing function badCharHeuristic ( ) for given pattern ; int s = 0 ; s is shift of the pattern with respect to text ; Keep reducing index j of pattern while characters of pattern and text are matching at this shift s ; If the pattern is present at current shift , then index j will become - 1 after the above loop ; Shift the pattern so that the next character in text aligns with the last occurrence of it in pattern . The condition s + m < n is necessary for the case when pattern occurs at the end of text ; Shift the pattern so that the bad character in text aligns with the last occurrence of it in pattern . The max function is used to make sure that we get a positive shift . We may get a negative shift if the last occurrence of bad character in pattern is on the right side of the current character . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; # define NO_OF_CHARS  256 NEW_LINE void badCharHeuristic ( string str , int size , int badchar [ NO_OF_CHARS ] ) { int i ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) badchar [ i ] = -1 ; for ( i = 0 ; i < size ; i ++ ) badchar [ ( int ) str [ i ] ] = i ; } void search ( string txt , string pat ) { int m = pat . size ( ) ; int n = txt . size ( ) ; int badchar [ NO_OF_CHARS ] ; badCharHeuristic ( pat , m , badchar ) ; while ( s <= ( n - m ) ) { int j = m - 1 ; while ( j >= 0 && pat [ j ] == txt [ s + j ] ) j -- ; if ( j < 0 ) { cout << " pattern ▁ occurs ▁ at ▁ shift ▁ = ▁ " << s << endl ; s += ( s + m < n ) ? m - badchar [ txt [ s + m ] ] : 1 ; } else s += max ( 1 , j - badchar [ txt [ s + j ] ] ) ; } } int main ( ) { string txt = " ABAAABCD " ; string pat = " ABC " ; search ( txt , pat ) ; return 0 ; }
Sum of nodes at maximum depth of a Binary Tree | C ++ code for sum of nodes at maximum depth ; Constructor ; function to find the sum of nodes at maximum depth arguments are node and max , where max is to match the depth of node at every call to node , if max will be equal to 1 , means we are at deepest node . ; base case ; max == 1 to track the node at deepest level ; recursive call to left and right nodes ; maxDepth function to find the max depth of the tree ; base case ; either leftDepth of rightDepth is greater add 1 to include height of node at which call is ; call to function to calculate max depth ; Driver code ; Constructing tree ; call to calculate required sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; Node ( int data ) { this -> data = data ; this -> left = NULL ; this -> right = NULL ; } } ; int sumMaxLevelRec ( Node * node , int max ) { if ( node == NULL ) return 0 ; if ( max == 1 ) return node -> data ; return sumMaxLevelRec ( node -> left , max - 1 ) + sumMaxLevelRec ( node -> right , max - 1 ) ; } int maxDepth ( Node * node ) { if ( node == NULL ) return 0 ; return 1 + max ( maxDepth ( node -> left ) , maxDepth ( node -> right ) ) ; } int sumMaxLevel ( Node * root ) { int MaxDepth = maxDepth ( root ) ; return sumMaxLevelRec ( root , MaxDepth ) ; } int main ( ) { Node * root = new Node ( 1 ) ; root -> left = new Node ( 2 ) ; root -> right = new Node ( 3 ) ; root -> left -> left = new Node ( 4 ) ; root -> left -> right = new Node ( 5 ) ; root -> right -> left = new Node ( 6 ) ; root -> right -> right = new Node ( 7 ) ; cout << ( sumMaxLevel ( root ) ) << endl ; }