codeparrot/xlcost-text-to-code huggingface dataset:
Check whether a given graph is Bipartite or not |  ;  ; vector to store colour of vertex assiging all to - 1 i . e . uncoloured colours are either 0 or 1 for understanding take 0 as red and 1 as blue ; queue for BFS storing { vertex , colour } ; loop incase graph is not connected ; if not coloured ; colouring with 0 i . e . red ; current vertex ; colour of current vertex ; traversing vertexes connected to current vertex ; if already coloured with parent vertex color then bipartite graph is not possible ; if uncooloured ; colouring with opposite color to that of parent ; if all vertexes are coloured such that no two connected vertex have same colours ; { Driver Code Starts . ; adjacency list for storing graph ; returns 1 if bipatite graph is possible ; returns 0 if bipartite graph is not possible
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isBipartite ( int V , vector < int > adj [ ] ) { vector < int > col ( V , -1 ) ; queue < pair < int , int > > q ; for ( int i = 0 ; i < V ; i ++ ) { if ( col [ i ] == -1 ) { q . push ( { i , 0 } ) ; col [ i ] = 0 ; while ( ! q . empty ( ) ) { pair < int , int > p = q . front ( ) ; q . pop ( ) ; int v = p . first ; int c = p . second ; for ( int j : adj [ v ] ) { if ( col [ j ] == c ) return 0 ; if ( col [ j ] == -1 ) { col [ j ] = ( c ) ? 0 : 1 ; q . push ( { j , col [ j ] } ) ; } } } } } return 1 ; } int main ( ) { int V , E ; V = 4 , E = 8 ; vector < int > adj [ V ] ; adj [ 0 ] = { 1 , 3 } ; adj [ 1 ] = { 0 , 2 } ; adj [ 2 ] = { 1 , 3 } ; adj [ 3 ] = { 0 , 2 } ; bool ans = isBipartite ( V , adj ) ; if ( ans ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Lexicographically smallest string whose hamming distance from given string is exactly K | CPP program to find Lexicographically smallest string whose hamming distance from given string is exactly K ; function to find Lexicographically smallest string with hamming distance k ; If k is 0 , output input string ; Copying input string into output string ; Traverse all the character of the string ; If current character is not ' a ' ; copy character ' a ' to output string ; If hamming distance became k , break ; ; If k is less than p ; Traversing string in reverse order ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findString ( string str , int n , int k ) { if ( k == 0 ) { cout << str << endl ; return ; } string str2 = str ; int p = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str2 [ i ] != ' a ' ) { str2 [ i ] = ' a ' ; p ++ ; if ( p == k ) break ; } } if ( p < k ) { for ( int i = n - 1 ; i >= 0 ; i -- ) if ( str [ i ] == ' a ' ) { str2 [ i ] = ' b ' ; p ++ ; if ( p == k ) break ; } } cout << str2 << endl ; } int main ( ) { string str = " pqrs " ; int n = str . length ( ) ; int k = 2 ; findString ( str , n , k ) ; return 0 ; }
Lexicographically first alternate vowel and consonant string | C ++ implementation of lexicographically first alternate vowel and consonant string ; ' ch ' is vowel or not ; create alternate vowel and consonant string str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required lexicographically first alternate vowel and consonant string ; hash table to store frequencies of each character in ' str ' ; initialize all elements of char_freq [ ] to 0 ; count vowels ; count consonants ; update frequency of ' ch ' in char_freq [ ] ; no such string can be formed ; form the vowel string ' vstr ' and consonant string ' cstr ' which contains characters in lexicographical order ; remove first character of vowel string then create alternate string with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant string then create alternate string with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating string with consonant ; start creating string with vowel ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  26 NEW_LINE bool isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; return false ; } string createAltStr ( string str1 , string str2 , int start , int l ) { string finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) finalStr = ( finalStr + str1 . at ( i ) ) + str2 . at ( j ) ; return finalStr ; } string findAltStr ( string str ) { int char_freq [ SIZE ] ; memset ( char_freq , 0 , sizeof ( char_freq ) ) ; int nv = 0 , nc = 0 ; string vstr = " " , cstr = " " ; int l = str . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str . at ( i ) ; if ( isVowel ( ch ) ) nv ++ ; else nc ++ ; char_freq [ ch - 97 ] ++ ; } if ( abs ( nv - nc ) >= 2 ) return " no ▁ such ▁ string " ; for ( int i = 0 ; i < SIZE ; i ++ ) { char ch = ( char ) ( i + 97 ) ; for ( int j = 1 ; j <= char_freq [ i ] ; j ++ ) { if ( isVowel ( ch ) ) vstr += ch ; else cstr += ch ; } } if ( nv > nc ) return ( vstr . at ( 0 ) + createAltStr ( cstr , vstr , 1 , nv ) ) ; if ( nc > nv ) return ( cstr . at ( 0 ) + createAltStr ( vstr , cstr , 1 , nc ) ) ; if ( cstr . at ( 0 ) < vstr . at ( 0 ) ) return createAltStr ( cstr , vstr , 0 , nv ) ; return createAltStr ( vstr , cstr , 0 , nc ) ; } int main ( ) { string str = " aeroplane " ; cout << findAltStr ( str ) ; return 0 ; }
Decode an Encoded Base 64 String to ASCII String | C ++ Program to decode a base64 Encoded string back to ASCII string ; char_set = " ABCDEFGHIJKLMNOPQRSTUVWXYZ ▁ abcdefghijklmnopqrstuvwxyz0123456789 + / " ; stores the bitstream . ; count_bits stores current number of bits in num . ; selects 4 characters from encoded string at a time . find the position of each encoded character in char_set and stores in num . ; make space for 6 bits . ; encoded [ i + j ] = ' E ' , ' E ' - ' A ' = 5 ' E ' has 5 th position in char_set . ; encoded [ i + j ] = ' e ' , ' e ' - ' a ' = 5 , 5 + 26 = 31 , ' e ' has 31 st position in char_set . ; encoded [ i + j ] = '8' , '8' - '0' = 8 8 + 52 = 60 , '8' has 60 th position in char_set . ; ' + ' occurs in 62 nd position in char_set . ; ' / ' occurs in 63 rd position in char_set . ; ( str [ i + j ] == ' = ' ) remove 2 bits to delete appended bits during encoding . ; 255 in binary is 11111111 ; Driver code ; Do not count last NULL character .
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define SIZE  100 NEW_LINE char * base64Decoder ( char encoded [ ] , int len_str ) { char * decoded_string ; decoded_string = ( char * ) malloc ( sizeof ( char ) * SIZE ) ; int i , j , k = 0 ; int num = 0 ; int count_bits = 0 ; for ( i = 0 ; i < len_str ; i += 4 ) { num = 0 , count_bits = 0 ; for ( j = 0 ; j < 4 ; j ++ ) { if ( encoded [ i + j ] != ' = ' ) { num = num << 6 ; count_bits += 6 ; } if ( encoded [ i + j ] >= ' A ' && encoded [ i + j ] <= ' Z ' ) num = num | ( encoded [ i + j ] - ' A ' ) ; else if ( encoded [ i + j ] >= ' a ' && encoded [ i + j ] <= ' z ' ) num = num | ( encoded [ i + j ] - ' a ' + 26 ) ; else if ( encoded [ i + j ] >= '0' && encoded [ i + j ] <= '9' ) num = num | ( encoded [ i + j ] - '0' + 52 ) ; else if ( encoded [ i + j ] == ' + ' ) num = num | 62 ; else if ( encoded [ i + j ] == ' / ' ) num = num | 63 ; else { num = num >> 2 ; count_bits -= 2 ; } } while ( count_bits != 0 ) { count_bits -= 8 ; decoded_string [ k ++ ] = ( num >> count_bits ) & 255 ; } } decoded_string [ k ] = ' \0' ; return decoded_string ; } int main ( ) { char encoded_string [ ] = " TUVOT04 = " ; int len_str = sizeof ( encoded_string ) / sizeof ( encoded_string [ 0 ] ) ; len_str -= 1 ; cout << " Encoded ▁ string ▁ : ▁ " << encoded_string << endl ; cout << " Decoded ▁ string ▁ : ▁ " << base64Decoder ( encoded_string , len_str ) << endl ; return 0 ; }
Print all possible strings that can be made by placing spaces | C ++ program to print all strings that can be made by placing spaces ; Function to print all subsequences ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSubsequences ( string str ) { int n = str . length ( ) ; unsigned int opsize = pow ( 2 , n - 1 ) ; for ( int counter = 0 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << str [ j ] ; if ( counter & ( 1 << j ) ) cout << " ▁ " ; } cout << endl ; } } int main ( ) { string str = " ABC " ; printSubsequences ( str ) ; return 0 ; }
Find n | C ++ program to print n - th permutation ; Utility for calculating factorials ; Function for nth permutation ; Length of given string ; Count frequencies of all characters ; Out string for output string ; Iterate till sum equals n ; We update both n and sum in this loop . ; Check for characters present in freq [ ] ; Remove character ; Calculate sum after fixing a particular char ; if sum > n fix that char as present char and update sum and required nth after fixing char at that position ; if sum < n , add character back ; if sum == n means this char will provide its greatest permutation as nth permutation ; append string termination character and print result ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE const int MAX_CHAR = 26 ; const int MAX_FACT = 20 ; ll fact [ MAX_FACT ] ; void precomputeFactorials ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX_FACT ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } void nPermute ( char str [ ] , int n ) { precomputeFactorials ( ) ; int len = strlen ( str ) ; int freq [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < len ; i ++ ) freq [ str [ i ] - ' a ' ] ++ ; char out [ MAX_CHAR ] ; int sum = 0 ; int k = 0 ; while ( sum != n ) { sum = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; int xsum = fact [ len - 1 - k ] ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) xsum /= fact [ freq [ j ] ] ; sum += xsum ; if ( sum >= n ) { out [ k ++ ] = i + ' a ' ; n -= ( sum - xsum ) ; break ; } if ( sum < n ) freq [ i ] ++ ; } } for ( int i = MAX_CHAR - 1 ; k < len && i >= 0 ; i -- ) if ( freq [ i ] ) { out [ k ++ ] = i + ' a ' ; freq [ i ++ ] -- ; } out [ k ] = ' \0' ; cout << out ; } int main ( ) { int n = 2 ; char str [ ] = " geeksquiz " ; nPermute ( str , n ) ; return 0 ; }
Minimum number of deletions so that no two consecutive are same | CPP code to count minimum deletions required so that there are no consecutive characters left ; Function for counting deletions ; If two consecutive characters are the same , delete one of them . ; Driver code ; Function call to print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDeletions ( string str ) { int ans = 0 ; for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) if ( str [ i ] == str [ i + 1 ] ) ans ++ ; return ans ; } int main ( ) { string str = " AAABBB " ; cout << countDeletions ( str ) ; return 0 ; }
Count words that appear exactly two times in an array of words | C ++ program to count all words with count exactly 2. ; Returns count of words with frequency exactly 2. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countWords ( string str [ ] , int n ) { unordered_map < string , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ str [ i ] ] += 1 ; int res = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) if ( ( it -> second == 2 ) ) res ++ ; return res ; } int main ( ) { string s [ ] = { " hate " , " love " , " peace " , " love " , " peace " , " hate " , " love " , " peace " , " love " , " peace " } ; int n = sizeof ( s ) / sizeof ( s [ 0 ] ) ; cout << countWords ( s , n ) ; return 0 ; }
Check whether a given graph is Bipartite or not | C ++ program to find out whether a given graph is Bipartite or not . Using recursion . ; color this pos as c and all its neighbours and 1 - c ; start is vertex 0 ; ; two colors 1 and 0 ; Driver Code
#include <iostream> NEW_LINE using namespace std ; #define V  4 NEW_LINE bool colorGraph ( int G [ ] [ V ] , int color [ ] , int pos , int c ) { if ( color [ pos ] != -1 && color [ pos ] != c ) return false ; color [ pos ] = c ; bool ans = true ; for ( int i = 0 ; i < V ; i ++ ) { if ( G [ pos ] [ i ] ) { if ( color [ i ] == -1 ) ans &= colorGraph ( G , color , i , 1 - c ) ; if ( color [ i ] != -1 && color [ i ] != 1 - c ) return false ; } if ( ! ans ) return false ; } return true ; } bool isBipartite ( int G [ ] [ V ] ) { int color [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = -1 ; int pos = 0 ; return colorGraph ( G , color , pos , 1 ) ; } int main ( ) { int G [ ] [ V ] = { { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; isBipartite ( G ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Nth Even length Palindrome | C ++ program to find n = th even length string . ; Function to find nth even length Palindrome ; string r to store resultant palindrome . Initialize same as s ; In this loop string r stores reverse of string s after the string s in consecutive manner . ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string evenlength ( string n ) { string res = n ; for ( int j = n . length ( ) - 1 ; j >= 0 ; -- j ) res += n [ j ] ; return res ; } int main ( ) { string n = "10" ; cout << evenlength ( n ) ; return 0 ; }
Determine if a string has all Unique Characters | C ++ program to illustrate string with unique characters using brute force technique ; If at any time we encounter 2 same characters , return false ; If no duplicate characters encountered , return true ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool uniqueCharacters ( string str ) { for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < str . length ( ) ; j ++ ) { if ( str [ i ] == str [ j ] ) { return false ; } } } return true ; } int main ( ) { string str = " GeeksforGeeks " ; if ( uniqueCharacters ( str ) ) { cout << " The ▁ String ▁ " << str << " ▁ has ▁ all ▁ unique ▁ characters STRNEWLINE " ; } else { cout << " The ▁ String ▁ " << str << " ▁ has ▁ duplicate ▁ characters STRNEWLINE " ; } return 0 ; }
Check if given string can be split into four distinct strings | C ++ program to check if we can break a string into four distinct strings . ; Return if the given string can be split or not . ; We can always break a string of size 10 or more into four distinct strings . ; Brute Force ; Making 4 string from the given string ; Checking if they are distinct or not . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( string s ) { if ( s . size ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . size ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . size ( ) ; k ++ ) { string s1 = s . substr ( 0 , i ) ; string s2 = s . substr ( i , j - i ) ; string s3 = s . substr ( j , k - j ) ; string s4 = s . substr ( k , s . size ( ) - k ) ; if ( s1 != s2 && s1 != s3 && s1 != s4 && s2 != s3 && s2 != s4 && s3 != s4 ) return true ; } } } return false ; } int main ( ) { string str = " aaabb " ; ( check ( str ) ) ? ( cout << " Yes " << endl ) : ( cout << " No " << endl ) ; return 0 ; }
Multiply Large Numbers represented as Strings | C ++ program to multiply two numbers represented as strings . ; Multiplies str1 and str2 , and prints result . ; will keep the result number in vector in reverse order ; Below two indexes are used to find positions in result . ; Go from right to left in num1 ; To shift position to left after every multiplication of a digit in num2 ; Go from right to left in num2 ; Take current digit of second number ; Multiply with current digit of first number and add result to previously stored result at current position . ; Carry for next iteration ; Store result ; store carry in next cell ; To shift position to left after every multiplication of a digit in num1 . ; ignore '0' s from the right ; If all were '0' s - means either both or one of num1 or num2 were '0' ; generate the result string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string multiply ( string num1 , string num2 ) { int len1 = num1 . size ( ) ; int len2 = num2 . size ( ) ; if ( len1 == 0 len2 == 0 ) return "0" ; vector < int > result ( len1 + len2 , 0 ) ; int i_n1 = 0 ; int i_n2 = 0 ; for ( int i = len1 - 1 ; i >= 0 ; i -- ) { int carry = 0 ; int n1 = num1 [ i ] - '0' ; i_n2 = 0 ; for ( int j = len2 - 1 ; j >= 0 ; j -- ) { int n2 = num2 [ j ] - '0' ; int sum = n1 * n2 + result [ i_n1 + i_n2 ] + carry ; carry = sum / 10 ; result [ i_n1 + i_n2 ] = sum % 10 ; i_n2 ++ ; } if ( carry > 0 ) result [ i_n1 + i_n2 ] += carry ; i_n1 ++ ; } int i = result . size ( ) - 1 ; while ( i >= 0 && result [ i ] == 0 ) i -- ; if ( i == -1 ) return "0" ; string s = " " ; while ( i >= 0 ) s += std :: to_string ( result [ i -- ] ) ; return s ; } int main ( ) { string str1 = "1235421415454545454545454544" ; string str2 = "1714546546546545454544548544544545" ; if ( ( str1 . at ( 0 ) == ' - ' || str2 . at ( 0 ) == ' - ' ) && ( str1 . at ( 0 ) != ' - ' || str2 . at ( 0 ) != ' - ' ) ) cout << " - " ; if ( str1 . at ( 0 ) == ' - ' ) str1 = str1 . substr ( 1 ) ; if ( str2 . at ( 0 ) == ' - ' ) str2 = str2 . substr ( 1 ) ; cout << multiply ( str1 , str2 ) ; return 0 ; }
Find an equal point in a string of brackets | C ++ program to find an index k which decides the number of opening brackets is equal to the number of closing brackets ; Function 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findIndex ( string str ) { int len = str . length ( ) ; int open [ len + 1 ] , close [ len + 1 ] ; int index = -1 ; memset ( open , 0 , sizeof ( open ) ) ; memset ( close , 0 , sizeof ( close ) ) ; open [ 0 ] = 0 ; close [ len ] = 0 ; if ( str [ 0 ] == ' ( ' ) open [ 1 ] = 1 ; if ( str [ len - 1 ] == ' ) ' ) close [ len - 1 ] = 1 ; for ( int i = 1 ; i < len ; i ++ ) { if ( str [ i ] == ' ( ' ) open [ i + 1 ] = open [ i ] + 1 ; else open [ i + 1 ] = open [ i ] ; } for ( int 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 ( int i = 0 ; i <= len ; i ++ ) if ( open [ i ] == close [ i ] ) index = i ; return index ; } int main ( ) { string str = " ( ( ) ) ) ( ( ) ( ) ( ) ) ) ) " ; cout << findIndex ( str ) ; return 0 ; }
Convert decimal fraction to binary number | C ++ program to convert fractional decimal 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 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string decimalToBinary ( double num , int k_prec ) { string binary = " " ; int Integral = num ; double fractional = num - Integral ; while ( Integral ) { int rem = Integral % 2 ; binary . push_back ( rem + '0' ) ; Integral /= 2 ; } reverse ( binary . begin ( ) , binary . end ( ) ) ; binary . push_back ( ' . ' ) ; while ( k_prec -- ) { fractional *= 2 ; int fract_bit = fractional ; if ( fract_bit == 1 ) { fractional -= fract_bit ; binary . push_back ( 1 + '0' ) ; } else binary . push_back ( 0 + '0' ) ; } return binary ; } int main ( ) { double n = 4.47 ; int k = 3 ; cout << decimalToBinary ( n , k ) << " STRNEWLINE " ; n = 6.986 , k = 5 ; cout << decimalToBinary ( n , k ) ; return 0 ; }
Difference of two large numbers | C ++ program to find 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 ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSmaller ( string str1 , string str2 ) { int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; if ( n1 < n2 ) return true ; if ( n2 < n1 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( str1 [ i ] < str2 [ i ] ) return true ; else if ( str1 [ i ] > str2 [ i ] ) return false ; } return false ; } string findDiff ( string str1 , string str2 ) { if ( isSmaller ( str1 , str2 ) ) swap ( str1 , str2 ) ; string str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; int diff = n1 - n2 ; int carry = 0 ; for ( int i = n2 - 1 ; i >= 0 ; i -- ) { int sub = ( ( str1 [ i + diff ] - '0' ) - ( str2 [ i ] - '0' ) - carry ) ; if ( sub < 0 ) { sub = sub + 10 ; carry = 1 ; } else carry = 0 ; str . push_back ( sub + '0' ) ; } for ( int i = n1 - n2 - 1 ; i >= 0 ; i -- ) { if ( str1 [ i ] == '0' && carry ) { str . push_back ( '9' ) ; continue ; } int sub = ( ( str1 [ i ] - '0' ) - carry ) ; str . push_back ( sub + '0' ) ; carry = 0 ; } reverse ( str . begin ( ) , str . end ( ) ) ; return str ; } int main ( ) { string str1 = "88" ; string str2 = "1079" ; cout << findDiff ( str1 , str2 ) ; return 0 ; }
Efficiently check if a string has all unique characters without using any additional data structure | Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool unique ( string s ) { sort ( s . begin ( ) , s . end ( ) ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == s [ i + 1 ] ) { return false ; break ; } } return true ; } int main ( ) { if ( unique ( " abcdd " ) == true ) { cout << " String ▁ is ▁ Unique " << endl ; } else { cout << " String ▁ is ▁ not ▁ Unique " << endl ; } return 0 ; }
Check if two strings are k | Optimized C ++ 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 length 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX_CHAR = 26 ; bool areKAnagrams ( string str1 , string str2 , int k ) { int n = str1 . length ( ) ; if ( str2 . length ( ) != n ) return false ; int hash_str1 [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) hash_str1 [ str1 [ i ] - ' a ' ] ++ ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash_str1 [ str2 [ i ] - ' a ' ] > 0 ) hash_str1 [ str2 [ i ] - ' a ' ] -- ; else count ++ ; if ( count > k ) return false ; } return true ; } int main ( ) { string str1 = " fodr " ; string str2 = " gork " ; int k = 2 ; if ( areKAnagrams ( str1 , str2 , k ) == true ) cout << " Yes " ; else cout << " No " ; return 0 ; }