codeparrot/xlcost-text-to-code huggingface dataset:
Round the given number to nearest multiple of 10 | Set | Function to round the given number to the nearest multiple of 10 ; If string is empty ; If the last digit is less then or equal to 5 then it can be rounded to the nearest ( previous ) multiple of 10 by just replacing the last digit with 0 ; Set the last digit to 0 ; Print the updated number ; The number hast to be rounded to the next multiple of 10 ; To store the carry ; Replace the last digit with 0 ; Starting from the second last digit , add 1 to digits while there is carry ; While there are digits to consider and there is carry to add ; Get the current digit ; Add the carry ; If the digit exceeds 9 then the carry will be generated ; Else there will be no carry ; Update the current digit ; Get to the previous digit ; If the carry is still 1 then it must be inserted at the beginning of the string ; Prin the rest of the number ; Driver code
def roundToNearest ( str , n ) : NEW_LINE INDENT if ( str == " " ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( ord ( str [ n - 1 ] ) - ord ( '0' ) <= 5 ) : NEW_LINE INDENT str = list ( str ) NEW_LINE str [ n - 1 ] = '0' NEW_LINE str = ' ' . join ( str ) NEW_LINE print ( str [ 0 : n ] ) NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 NEW_LINE str = list ( str ) NEW_LINE str [ n - 1 ] = '0' NEW_LINE str = ' ' . join ( str ) NEW_LINE i = n - 2 NEW_LINE carry = 1 NEW_LINE while ( i >= 0 and carry == 1 ) : NEW_LINE INDENT currentDigit = ord ( str [ i ] ) - ord ( '0' ) NEW_LINE currentDigit += carry NEW_LINE if ( currentDigit > 9 ) : NEW_LINE INDENT carry = 1 NEW_LINE currentDigit = 0 NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 NEW_LINE DEDENT str [ i ] = chr ( currentDigit + '0' ) NEW_LINE i -= 1 NEW_LINE DEDENT if ( carry == 1 ) : NEW_LINE INDENT print ( carry ) NEW_LINE DEDENT print ( str [ 0 : n ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "99999999999999993" NEW_LINE n = len ( str ) NEW_LINE roundToNearest ( str , n ) NEW_LINE DEDENT
Repeat substrings of the given String required number of times | Function that returns true if the passed character is a digit ; Function to return the next index of a non - digit character in the string starting at the index i ( returns - 1 ifno such index is found ) ; If the character at index i is a digit then skip to the next character ; If no such index was found ; Function to append str the given number of times to the StringBuilder ; Function to return the string after performing the given operations ; To build the resultant string ; Index of the first non - digit character in the string ; While there are substrings that do not consist of digits ; Find the ending of the substring ; Starting index of the number ; If no digit appears after the current substring ; Find the index at which the current number ends ; Parse the number from the substring ; Repeat the current substring required number of times ; Find the next non - digit character index ; Return the resultant string ; Driver code
def isDigit ( ch ) : NEW_LINE INDENT if ch >= '0' and ch <= '9' : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def nextNonDigit ( string , i ) : NEW_LINE INDENT while i < len ( string ) and isDigit ( string [ i ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if i >= len ( string ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return i NEW_LINE DEDENT def appendRepeated ( sb , string , times ) : NEW_LINE INDENT for i in range ( times ) : NEW_LINE INDENT sb . append ( string ) NEW_LINE DEDENT DEDENT def findString ( string , n ) : NEW_LINE INDENT sb = list ( ) NEW_LINE startStr = nextNonDigit ( string , 0 ) NEW_LINE while startStr != - 1 : NEW_LINE INDENT endStr = startStr NEW_LINE while ( endStr + 1 < n and not isDigit ( string [ endStr + 1 ] ) ) : NEW_LINE INDENT endStr += 1 NEW_LINE DEDENT startNum = endStr + 1 NEW_LINE if startNum == - 1 : NEW_LINE INDENT break NEW_LINE DEDENT endNum = startNum NEW_LINE while ( endNum + 1 < n and isDigit ( string [ endNum + 1 ] ) ) : NEW_LINE INDENT endNum += 1 NEW_LINE DEDENT num = int ( string [ startNum : endNum + 1 ] ) NEW_LINE appendRepeated ( sb , string [ startStr : endStr + 1 ] , num ) NEW_LINE startStr = nextNonDigit ( string , endStr + 1 ) NEW_LINE DEDENT sb = ' ' . join ( sb ) NEW_LINE return sb NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " g1ee1ks1for1g1e2ks1" NEW_LINE n = len ( string ) NEW_LINE print ( findString ( string , n ) ) NEW_LINE DEDENT
Check if the given string is vowel prime | Function that returns true if c is a vowel ; Function that returns true if all the vowels in the given string are only at prime indices ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries in it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; For every character of the given String ; If current character is vowel and the index is not prime ; Driver code
def isVowel ( c ) : NEW_LINE INDENT if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isVowelPrime ( Str , n ) : NEW_LINE INDENT prime = [ True for i in range ( n ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , n ) : NEW_LINE INDENT if p * p > n : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , n , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( isVowel ( Str [ i ] ) and prime [ i ] == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT Str = " geeksforgeeks " ; NEW_LINE n = len ( Str ) NEW_LINE if ( isVowelPrime ( Str , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Swap all occurrences of two characters to get lexicographically smallest string | python3 implementation of the approach ; Function to return the lexicographically smallest after swapping all the occurrences of any two characters ; To store the first index of every character of str ; Store the first occurring index every character ; If current character is appearing for the first time in str ; Starting from the leftmost character ; For every character smaller than ord ( str [ i ] ) ; If there is a character in str which is smaller than ord ( str [ i ] ) and appears after it ; If the required character pair is found ; If swapping is possible ; Characters to be swapped ; For every character ; Replace every ch1 with ch2 and every ch2 with ch1 ; Driver code
MAX = 256 NEW_LINE def smallestStr ( str , n ) : NEW_LINE INDENT i , j = 0 , 0 NEW_LINE chk = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT chk [ i ] = - 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( chk [ ord ( str [ i ] ) ] == - 1 ) : NEW_LINE INDENT chk [ ord ( str [ i ] ) ] = i NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT flag = False NEW_LINE for j in range ( ord ( str [ i ] ) ) : NEW_LINE INDENT if ( chk [ j ] > chk [ ord ( str [ i ] ) ] ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( i < n ) : NEW_LINE INDENT ch1 = ( str [ i ] ) NEW_LINE ch2 = chr ( j ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == ch1 ) : NEW_LINE INDENT str [ i ] = ch2 NEW_LINE DEDENT elif ( str [ i ] == ch2 ) : NEW_LINE INDENT str [ i ] = ch1 NEW_LINE DEDENT DEDENT DEDENT return " " . join ( str ) NEW_LINE DEDENT st = " ccad " NEW_LINE str = [ i for i in st ] NEW_LINE n = len ( str ) NEW_LINE print ( smallestStr ( str , n ) ) NEW_LINE
Longest sub string of 0 's in a binary string which is repeated K times | Function to find the longest subof 0 's ; To store size of the string ; To store the required answer ; Find the longest subof 0 's ; Run a loop upto s [ i ] is zero ; Check the conditions ; Driver code ; Function call
def longest_substring ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if ( k > 1 ) : NEW_LINE INDENT s += s NEW_LINE n *= 2 NEW_LINE DEDENT ans = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT x = 0 NEW_LINE while ( i < n and s [ i ] == '0' ) : NEW_LINE INDENT x , i = x + 1 , i + 1 NEW_LINE DEDENT ans = max ( ans , x ) NEW_LINE i += 1 NEW_LINE DEDENT if ( k == 1 or ans != n ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return ( ans // 2 ) * k NEW_LINE DEDENT DEDENT s = "010001000" NEW_LINE k = 4 NEW_LINE print ( longest_substring ( s , k ) ) NEW_LINE
Find the number of occurrences of a character upto preceding position | Function to find the number of occurrences of a character at position P upto p - 1 ; Return the required count ; Driver code ; Function call
def Occurrence ( s , position ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( position - 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ position - 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT s = " ababababab " ; NEW_LINE p = 9 NEW_LINE print ( Occurrence ( s , p ) ) NEW_LINE
Find the number of occurrences of a character upto preceding position | Function to find the number of occurrences of a character at position P upto p - 1 ; Iterate over the string ; Store the Occurrence of same character ; Increase its frequency ; Return the required count ; Driver code ; Function call
def countOccurrence ( s , position ) : NEW_LINE INDENT alpha = [ 0 ] * 26 NEW_LINE b = [ 0 ] * len ( s ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT b [ i ] = alpha [ ord ( s [ i ] ) - 97 ] NEW_LINE alpha [ ord ( s [ i ] ) - 97 ] = alpha [ ord ( s [ i ] ) - 97 ] + 1 NEW_LINE DEDENT return b [ position - 1 ] NEW_LINE DEDENT s = " ababababab " NEW_LINE p = 9 NEW_LINE print ( countOccurrence ( s , p ) ) NEW_LINE
Map every character of one string to another such that all occurrences are mapped to the same character | Python 3 implementation of the approach ; Function that returns true if the mapping is possible ; Both the strings are of un - equal lengths ; To store the frequencies of the characters in both the string ; Update frequencies of the characters ; For every character of s1 ; If current character is not present in s1 ; Find a character in s2 that has frequency equal to the current character 's  frequency in s1 ; If such character is found ; Set the frequency to - 1 so that it doesn 't get picked again ; Set found to true ; If there is no character in s2 that could be mapped to the current character in s1 ; Driver code
MAX = 26 NEW_LINE def canBeMapped ( s1 , l1 , s2 , l2 ) : NEW_LINE INDENT if ( l1 != l2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT freq1 = [ 0 for i in range ( MAX ) ] NEW_LINE freq2 = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT freq1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT freq2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq1 [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT found = False NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT if ( freq1 [ i ] == freq2 [ j ] ) : NEW_LINE INDENT freq2 [ j ] = - 1 NEW_LINE found = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( found == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " axx " NEW_LINE s2 = " cbc " NEW_LINE l1 = len ( s1 ) NEW_LINE l2 = len ( s2 ) NEW_LINE if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Distinct strings such that they contains given strings as sub | Set to store strings and aduplicates ; Recursive function to generate the required strings ; If currentis part of the result ; Insert it into the set ; If character from str1 can be chosen ; If character from str2 can be chosen ; Function to print the generated strings from the set ; Driver code
stringSet = dict ( ) NEW_LINE def find_permutation ( str1 , str2 , len1 , len2 , i , j , res ) : NEW_LINE INDENT if ( len ( res ) == len1 + len2 ) : NEW_LINE INDENT stringSet [ res ] = 1 NEW_LINE return NEW_LINE DEDENT if ( i < len1 ) : NEW_LINE INDENT find_permutation ( str1 , str2 , len1 , len2 , i + 1 , j , res + str1 [ i ] ) NEW_LINE DEDENT if ( j < len2 ) : NEW_LINE INDENT find_permutation ( str1 , str2 , len1 , len2 , i , j + 1 , res + str2 [ j ] ) NEW_LINE DEDENT DEDENT def print_set ( ) : NEW_LINE INDENT for i in stringSet : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT str1 = " aa " NEW_LINE str2 = " ab " NEW_LINE len1 = len ( str1 ) NEW_LINE len2 = len ( str2 ) NEW_LINE find_permutation ( str1 , str2 , len1 , len2 , 0 , 0 , " " ) NEW_LINE print_set ( ) NEW_LINE
Generate all possible strings such that char at index i is either str1 [ i ] or str2 [ i ] | Recursive function to generate the required strings ; If length of the current string is equal to the length of the given strings then the current string is part of the result ; Choosing the current character from the string a ; Choosing the current character from the string b ; Driver code ; Third argument is an empty string that we will be appended in the recursion calls Fourth arguments is the length of the resultant string so far
def generateStr ( a , b , s , count , len ) : NEW_LINE INDENT if ( count == len ) : NEW_LINE INDENT print ( s ) ; NEW_LINE return ; NEW_LINE DEDENT generateStr ( a [ 1 : ] , b [ 1 : ] , s + a [ 0 ] , count + 1 , len ) ; NEW_LINE generateStr ( a [ 1 : ] , b [ 1 : ] , s + b [ 0 ] , count + 1 , len ) ; NEW_LINE DEDENT a = " abc " ; b = " def " ; NEW_LINE n = len ( a ) ; NEW_LINE generateStr ( a , b , " " , 0 , n ) ; NEW_LINE
Program to generate all possible valid IP addresses from given string | Set 2 | Function to get all the valid ip - addresses ; If index greater than givenString size and we have four block ; Remove the last dot ; Add ip - address to the results ; To add one index to ip - address ; Select one digit and call the same function for other blocks ; Backtrack to generate another possible ip address So we remove two index ( one for the digit and other for the dot ) from the end ; Select two consecutive digits and call the same function for other blocks ; Backtrack to generate another possible ip address So we remove three index from the end ; Select three consecutive digits and call the same function for other blocks ; Backtrack to generate another possible ip address So we remove four index from the end ; Driver code ; Fill result vector with all valid ip - addresses ; Print all the generated ip - addresses
def GetAllValidIpAddress ( result , givenString , index , count , ipAddress ) : NEW_LINE INDENT if ( len ( givenString ) == index and count == 4 ) : NEW_LINE INDENT ipAddress . pop ( ) ; NEW_LINE result . append ( ipAddress ) ; NEW_LINE return ; NEW_LINE DEDENT if ( len ( givenString ) < index + 1 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT ipAddress = ( ipAddress + givenString [ index : index + 1 ] + [ ' . ' ] ) ; NEW_LINE GetAllValidIpAddress ( result , givenString , index + 1 , count + 1 , ipAddress ) ; NEW_LINE ipAddress = ipAddress [ : - 2 ] ; NEW_LINE if ( len ( givenString ) < index + 2 or givenString [ index ] == '0' ) : NEW_LINE INDENT return ; NEW_LINE DEDENT ipAddress = ipAddress + givenString [ index : index + 2 ] + [ ' . ' ] ; NEW_LINE GetAllValidIpAddress ( result , givenString , index + 2 , count + 1 , ipAddress ) ; NEW_LINE ipAddress = ipAddress [ : - 3 ] ; NEW_LINE if ( len ( givenString ) < index + 3 or int ( " " . join ( givenString [ index : index + 3 ] ) ) > 255 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT ipAddress += givenString [ index : index + 3 ] + [ ' . ' ] ; NEW_LINE GetAllValidIpAddress ( result , givenString , index + 3 , count + 1 , ipAddress ) ; NEW_LINE ipAddress = ipAddress [ : - 4 ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT givenString = list ( "25525511135" ) ; NEW_LINE result = [ ] ; NEW_LINE GetAllValidIpAddress ( result , givenString , 0 , 0 , [ ] ) ; NEW_LINE for i in range ( len ( result ) ) : NEW_LINE INDENT print ( " " . join ( result [ i ] ) ) ; NEW_LINE DEDENT DEDENT
Count of non | Function to return the count of required non - overlapping sub - strings ; To store the required count ; If "010" matches the sub - string starting at current index i ; If "101" matches the sub - string starting at current index i ; Driver code
def countSubStr ( s , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE i = 0 NEW_LINE while i < ( n - 2 ) : NEW_LINE INDENT if ( s [ i ] == '0' and s [ i + 1 ] == '1' and s [ i + 2 ] == '0' ) : NEW_LINE INDENT count += 1 ; NEW_LINE i += 3 ; NEW_LINE DEDENT elif ( s [ i ] == '1' and s [ i + 1 ] == '0' and s [ i + 2 ] == '1' ) : NEW_LINE INDENT count += 1 ; NEW_LINE i += 3 ; NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "10101010101" ; NEW_LINE n = len ( s ) ; NEW_LINE print ( countSubStr ( s , n ) ) ; NEW_LINE DEDENT
Find distinct characters in distinct substrings of a string | Function to return the count of distinct characters in all the distinct sub - strings of the given string ; To store all the sub - strings ; To store the current sub - string ; To store the characters of the current sub - string ; If current sub - string hasn 't  been stored before ; Insert it into the set ; Update the count of distinct characters ; Driver code
def countTotalDistinct ( string ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE items = set ( ) ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT temp = " " ; NEW_LINE ans = set ( ) ; NEW_LINE for j in range ( i , len ( string ) ) : NEW_LINE INDENT temp = temp + string [ j ] ; NEW_LINE ans . add ( string [ j ] ) ; NEW_LINE if temp not in items : NEW_LINE INDENT items . add ( temp ) ; NEW_LINE cnt += len ( ans ) ; NEW_LINE DEDENT DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ABCA " ; NEW_LINE print ( countTotalDistinct ( string ) ) ; NEW_LINE DEDENT
Partition the string in two parts such that both parts have at least k different characters | Function to find the partition of the string such that both parts have at least k different characters ; Length of the string ; To check if the current character is already found ; Count number of different characters in the left part ; If current character is not already found , increase cnt by 1 ; If count becomes equal to k , we 've  got the first part, therefore,  store current index and break the loop ; Increment i by 1 ; Clear the map ; Assign cnt as 0 ; If the current character is not already found , increase cnt by 1 ; If cnt becomes equal to k , the second part also have k different characters so break it ; If the second part has less than k different characters , then print " Not ▁ Possible " ; Otherwise print both parts ; Driver code ; Function call
def division_of_string ( string , k ) : NEW_LINE INDENT n = len ( string ) ; NEW_LINE has = { } ; NEW_LINE cnt = 0 ; i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if string [ i ] not in has : NEW_LINE INDENT cnt += 1 ; NEW_LINE has [ string [ i ] ] = True ; NEW_LINE DEDENT if ( cnt == k ) : NEW_LINE INDENT ans = i ; NEW_LINE break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE has . clear ( ) ; NEW_LINE cnt = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( string [ i ] not in has ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE has [ string [ i ] ] = True ; NEW_LINE DEDENT if ( cnt == k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT if ( cnt < k ) : NEW_LINE INDENT print ( " Not ▁ possible " , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT i = 0 ; NEW_LINE while ( i <= ans ) : NEW_LINE INDENT print ( string [ i ] , end = " " ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT print ( ) ; NEW_LINE while ( i < n ) : NEW_LINE INDENT print ( string [ i ] , end = " " ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE k = 4 ; NEW_LINE division_of_string ( string , k ) ; NEW_LINE DEDENT
Count substrings that contain all vowels | SET 2 | Function that returns true if c is a vowel ; Function to return the count of sub - strings that contain every vowel at least once and no consonant ; Map is used to store count of each vowel ; Start index is set to 0 initially ; If substring till now have all vowels atleast once increment start index until there are all vowels present between ( start , i ) and add n - i each time ; Function to extract all maximum length sub - strings in s that contain only vowels and then calls the countSubstringsUtil ( ) to find the count of valid sub - strings in that string ; If current character is a vowel then append it to the temp string ; The sub - string containing all vowels ends here ; If there was a valid sub - string ; Reset temp string ; For the last valid sub - string ; Driver code
def isVowel ( c ) : NEW_LINE INDENT return ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) ; NEW_LINE DEDENT def countSubstringsUtil ( s ) : NEW_LINE INDENT count = 0 ; NEW_LINE mp = dict . fromkeys ( s , 0 ) ; NEW_LINE n = len ( s ) ; NEW_LINE start = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ s [ i ] ] += 1 ; NEW_LINE while ( mp [ ' a ' ] > 0 and mp [ ' e ' ] > 0 and mp [ ' i ' ] > 0 and mp [ ' o ' ] > 0 and mp [ ' u ' ] > 0 ) : NEW_LINE INDENT count += n - i ; NEW_LINE mp [ s [ start ] ] -= 1 ; NEW_LINE start += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def countSubstrings ( s ) : NEW_LINE INDENT count = 0 ; NEW_LINE temp = " " ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT temp += s [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( len ( temp ) > 0 ) : NEW_LINE INDENT count += countSubstringsUtil ( temp ) ; NEW_LINE DEDENT temp = " " ; NEW_LINE DEDENT DEDENT if ( len ( temp ) > 0 ) : NEW_LINE INDENT count += countSubstringsUtil ( temp ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aeouisddaaeeiouua " ; NEW_LINE print ( countSubstrings ( s ) ) ; NEW_LINE DEDENT
Remove first adjacent pairs of similar characters until possible | Function to remove adjacent duplicates ; Iterate for every character in the string ; If ans string is empty or its last character does not match with the current character then append this character to the string ; Matches with the previous one ; Return the answer ; Driver Code
def removeDuplicates ( S ) : NEW_LINE INDENT ans = " " ; NEW_LINE for it in S : NEW_LINE INDENT if ( ans == " " or ans [ - 1 ] != it ) : NEW_LINE INDENT ans += it ; NEW_LINE DEDENT elif ( ans [ - 1 ] == it ) : NEW_LINE INDENT ans = ans [ : - 1 ] ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " keexxllx " ; NEW_LINE print ( removeDuplicates ( string ) ) ; NEW_LINE DEDENT
Reverse individual words with O ( 1 ) extra space | Function to resturn the after reversing the individual words ; Pointer to the first character of the first word ; If the current word has ended ; Pointer to the last character of the current word ; Reverse the current word ; Pointer to the first character of the next word ; Driver code
def reverseWords ( Str ) : NEW_LINE INDENT start = 0 NEW_LINE for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] == ' ▁ ' or i == len ( Str ) - 1 ) : NEW_LINE INDENT end = i - 1 NEW_LINE if ( i == len ( Str ) - 1 ) : NEW_LINE INDENT end = i NEW_LINE DEDENT while ( start < end ) : NEW_LINE INDENT Str [ start ] , Str [ end ] = Str [ end ] , Str [ start ] NEW_LINE start += 1 NEW_LINE end -= 1 NEW_LINE DEDENT start = i + 1 NEW_LINE DEDENT DEDENT return " " . join ( Str ) NEW_LINE DEDENT Str = " Geeks ▁ for ▁ Geeks " NEW_LINE Str = [ i for i in Str ] NEW_LINE print ( reverseWords ( Str ) ) NEW_LINE