codeparrot/xlcost-text-to-code huggingface dataset:
Number of words in a camelcase sequence | Function to find the count of words in a CamelCase sequence ; Driver code
def countWords ( str ) : NEW_LINE INDENT count = 1 NEW_LINE for i in range ( 1 , len ( str ) - 1 ) : NEW_LINE INDENT if ( str [ i ] . isupper ( ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT str = " geeksForGeeks " ; NEW_LINE print ( countWords ( str ) ) NEW_LINE
Check whether frequency of characters in a string makes Fibonacci Sequence | Python3 program to check whether the frequency of characters in a string make Fibonacci Sequence ; Function to check if the frequencies are in Fibonacci series ; map to store the frequencies of character ; Vector to store first n fibonacci numbers ; Get the size of the map ; a and b are first and second terms of fibonacci series ; vector v contains elements of fibonacci series ; Compare vector elements with values in Map ; Driver code
from collections import defaultdict NEW_LINE def isFibonacci ( s ) : NEW_LINE INDENT m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT v = [ ] NEW_LINE n = len ( m ) NEW_LINE a = b = 1 NEW_LINE v . append ( a ) NEW_LINE v . append ( b ) NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT v . append ( a + b ) NEW_LINE c = a + b NEW_LINE a , b = b , c NEW_LINE DEDENT flag , i = 1 , 0 NEW_LINE for itr in sorted ( m ) : NEW_LINE INDENT if m [ itr ] != v [ i ] : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if flag == 1 : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abeeedd " NEW_LINE print ( isFibonacci ( s ) ) NEW_LINE DEDENT
Lexicographically smallest string formed by removing at most one character | Function to return the smallest string ; iterate the string ; first point where s [ i ] > s [ i + 1 ] ; append the string without i - th character in it ; leave the last character ; Driver Code
def smallest ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE ans = " " NEW_LINE for i in range ( l - 1 ) : NEW_LINE INDENT if ( s [ i ] > s [ i + 1 ] ) : NEW_LINE INDENT for j in range ( l ) : NEW_LINE INDENT if ( i != j ) : NEW_LINE INDENT ans += s [ j ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT ans = s [ 0 : l - 1 ] NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abcda " NEW_LINE print ( smallest ( s ) ) NEW_LINE DEDENT
Arrangement of the characters of a word such that all vowels are at odd places | Python3 program to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code
import math NEW_LINE def fact ( n ) : NEW_LINE INDENT f = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT f = f * i ; NEW_LINE DEDENT return f ; NEW_LINE DEDENT def npr ( n , r ) : NEW_LINE INDENT return fact ( n ) / fact ( n - r ) ; NEW_LINE DEDENT def countPermutations ( str ) : NEW_LINE INDENT even = math . floor ( len ( str ) / 2 ) ; NEW_LINE odd = len ( str ) - even ; NEW_LINE ways = 0 ; NEW_LINE freq = [ 0 ] * 26 ; NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT nvowels = ( freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ) ; NEW_LINE nconsonants = len ( str ) - nvowels ; NEW_LINE ways = ( npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ) ; NEW_LINE return int ( ways ) ; NEW_LINE DEDENT str = " geeks " ; NEW_LINE print ( countPermutations ( str ) ) ; NEW_LINE
Replace all consonants with nearest vowels in a string | Function to replace consonant with nearest vowels ; Driver code
def replacingConsonants ( s ) : NEW_LINE INDENT nVowel = " aaaeeeeiiiiioooooouuuuuuuu " NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT s = s . replace ( s [ i ] , nVowel [ ord ( s [ i ] ) - 97 ] ) NEW_LINE DEDENT return s NEW_LINE DEDENT s = " geeksforgeeks " ; NEW_LINE print ( replacingConsonants ( s ) ) ; NEW_LINE
Replace consonants with next immediate consonants alphabetically in a String | Function to check if a character is vowel or not ; Function that replaces consonant with next immediate consonant alphabatically ; Start traversing the string ; if character is z , than replace it with character b ; if the alphabet is not z ; replace the element with next immediate alphabet ; if next immediate alphabet is vowel , than take next 2 nd immediate alphabet ( since no two vowels occurs consecutively in alphabets ) hence no further checking is required ; Driver code
def isVowel ( ch ) : NEW_LINE INDENT if ( ch != ' a ' and ch != ' e ' and ch != ' i ' and ch != ' o ' and ch != ' u ' ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def replaceConsonants ( s ) : NEW_LINE INDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) == False ) : NEW_LINE INDENT if ( s [ i ] == ' z ' ) : NEW_LINE INDENT s [ i ] = ' b ' ; NEW_LINE DEDENT else : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) ; NEW_LINE if ( isVowel ( s [ i ] ) == True ) : NEW_LINE INDENT s [ i ] = chr ( ord ( s [ i ] ) + 1 ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return ' ' . join ( s ) ; NEW_LINE DEDENT s = " geeksforgeeks " ; NEW_LINE print ( replaceConsonants ( list ( s ) ) ) ; NEW_LINE
Remove characters from string that appears strictly less than K times | Python 3 program to reduce the string by removing the characters which appears less than k times ; Function to reduce the string by removing the characters which appears less than k times ; Hash table initialised to 0 ; Increment the frequency of the character ; create a new empty string ; Append the characters which appears more than equal to k times ; Driver Code
MAX_CHAR = 26 NEW_LINE def removeChars ( str , k ) : NEW_LINE INDENT hash = [ 0 ] * ( MAX_CHAR ) NEW_LINE n = len ( str ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT res = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( hash [ ord ( str [ i ] ) - ord ( ' a ' ) ] >= k ) : NEW_LINE INDENT res += str [ i ] NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE k = 2 NEW_LINE print ( removeChars ( str , k ) ) NEW_LINE DEDENT
Count changes in Led Lights to display digits one by one | Python3 program to count number of on offs to display digits of a number . ; store the led lights required to display a particular number . ; compute the change in led and keep on adding the change ; Driver code
def countOnOff ( n ) : NEW_LINE INDENT Led = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 ] NEW_LINE leng = len ( n ) NEW_LINE sum = Led [ int ( n [ 0 ] ) - int ( '0' ) ] NEW_LINE for i in range ( 1 , leng ) : NEW_LINE INDENT sum = ( sum + abs ( Led [ int ( n [ i ] ) - int ( '0' ) ] - Led [ int ( n [ i - 1 ] ) - int ( '0' ) ] ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = "082" NEW_LINE print ( countOnOff ( n ) ) NEW_LINE DEDENT
Program to check if all characters have even frequency | Python implementation of the above approach ; creating a frequency array ; Finding length of s ; counting frequency of all characters ; checking if any odd frequency is there or not ; Driver code
def check ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - 97 ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] % 2 == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT s = " abaccaba " NEW_LINE if ( check ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Maximum Consecutive Zeroes in Concatenated Binary String | returns the maximum size of a substring consisting only of zeroes after k concatenation ; stores the maximum length of the required substring ; if the current character is 0 ; stores maximum length of current substrings with zeroes ; if the whole is filled with zero ; computes the length of the maximal prefix which contains only zeroes ; computes the length of the maximal suffix which contains only zeroes ; if more than 1 concatenations are to be made ; Driver code
def max_length_substring ( st , n , k ) : NEW_LINE INDENT max_len = 0 NEW_LINE len = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( st [ i ] == '0' ) : NEW_LINE INDENT len = len + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT len = 0 NEW_LINE DEDENT max_len = max ( max_len , len ) NEW_LINE DEDENT if ( max_len == n ) : NEW_LINE INDENT return n * k NEW_LINE DEDENT pref = 0 NEW_LINE suff = 0 NEW_LINE i = 0 NEW_LINE while ( st [ i ] == '0' ) : NEW_LINE INDENT i = i + 1 NEW_LINE pref = pref + 1 NEW_LINE DEDENT i = n - 1 NEW_LINE while ( st [ i ] == '0' ) : NEW_LINE INDENT i = i - 1 NEW_LINE suff = suff + 1 NEW_LINE DEDENT if ( k > 1 ) : NEW_LINE INDENT max_len = max ( max_len , pref + suff ) NEW_LINE DEDENT return max_len NEW_LINE DEDENT n = 6 NEW_LINE k = 3 NEW_LINE st = "110010" NEW_LINE ans = max_length_substring ( st , n , k ) NEW_LINE print ( ans ) NEW_LINE
Count number of substrings with numeric value greater than X | Function that counts valid sub - strings ; Only take those numbers that do not start with '0' . ; converting the sub - string starting from index ' i ' and having length ' len ' to int and checking if it is greater than X or not ; Driver code
def countSubStr ( S , X ) : NEW_LINE INDENT cnt = 0 NEW_LINE N = len ( S ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( S [ i ] != '0' ) : NEW_LINE INDENT j = 1 NEW_LINE while ( ( j + i ) <= N ) : NEW_LINE INDENT num = int ( S [ i : i + j ] ) NEW_LINE if ( num > X ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT j = j + 1 NEW_LINE DEDENT DEDENT DEDENT return cnt ; NEW_LINE DEDENT S = "2222" ; NEW_LINE X = 97 ; NEW_LINE print ( countSubStr ( S , X ) ) NEW_LINE
Check whether a binary string can be formed by concatenating given N numbers sequentially | Function that returns false if the number passed as argument contains digit ( s ) other than '0' or '1 ; Function that checks whether the binary string can be formed or not ; Empty string for storing the binary number ; check if a [ i ] can be a part of the binary string ; Conversion of int into string ; if a [ i ] can 't be a part  then break the loop ; possible to create binary string ; impossible to create binary string ; Driver code
' NEW_LINE def isBinary ( n ) : NEW_LINE INDENT while n != 0 : NEW_LINE INDENT temp = n % 10 NEW_LINE if temp != 0 and temp != 1 : NEW_LINE INDENT return False NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def formBinaryStr ( n , a ) : NEW_LINE INDENT flag = True NEW_LINE s = " " NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if isBinary ( a [ i ] ) == True : NEW_LINE INDENT s += str ( a [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT DEDENT if flag == True : NEW_LINE INDENT print ( s ) NEW_LINE DEDENT else : NEW_LINE INDENT cout <<   " - 1 NEW_LINE DEDENT DEDENT " NEW_LINE if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 10 , 1 , 0 , 11 , 10 ] NEW_LINE N = len ( a ) NEW_LINE formBinaryStr ( N , a ) NEW_LINE DEDENT
Check if all the palindromic sub | Function to check if the string is palindrome ; Function that checks whether all the palindromic sub - strings are of odd length . ; Creating each substring ; If the sub - string is of even length and is a palindrome then , we return False ; Driver code
def checkPalindrome ( s ) : NEW_LINE INDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] != s [ len ( s ) - i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def CheckOdd ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = " " NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT x += s [ j ] NEW_LINE if ( len ( x ) % 2 == 0 and checkPalindrome ( x ) == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE if ( CheckOdd ( s ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Number of permutations of a string in which all the occurrences of a given character occurs together | Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't  present in the string then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver code ; Assuming the string and the character are all in uppercase
def fact ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT result *= i NEW_LINE DEDENT return result NEW_LINE DEDENT def getResult ( string , ch ) : NEW_LINE INDENT has = [ 0 ] * 26 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT has [ ord ( string [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT particular = has [ ord ( ch ) - ord ( ' A ' ) ] NEW_LINE if particular == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT has [ ord ( ch ) - ord ( ' A ' ) ] = 0 NEW_LINE total = len ( string ) NEW_LINE total = total - particular + 1 NEW_LINE result = fact ( total ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if has [ i ] > 1 : NEW_LINE INDENT result /= fact ( has [ i ] ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " MISSISSIPPI " NEW_LINE print ( getResult ( string , ' S ' ) ) NEW_LINE DEDENT
Check if there exists any sub | Function to check if there exists at least 1 sub - sequence in a string which is not palindrome ; use set to count number of distinct characters ; insert each character in set ; If there is more than 1 unique characters , return true ; Else , return false ; Driver code
def isAnyNotPalindrome ( s ) : NEW_LINE INDENT unique = set ( ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT unique . add ( s [ i ] ) NEW_LINE DEDENT if ( len ( unique ) > 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aaaaab " NEW_LINE if ( isAnyNotPalindrome ( s ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
Arrangement of words without changing the relative position of vowel and consonants | this function return n ! ; this will return total number of ways ; freq maintains frequency of each character in word ; check character is vowel or not ; the characters that are not vowel must be consonant ; number of ways to arrange vowel ; multiply both as these are independent ; string contains only capital letters ; this will contain ans
def factorial ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def count ( word ) : NEW_LINE INDENT freq = [ 0 for i in range ( 30 ) ] NEW_LINE vowel = 0 NEW_LINE consonant = 0 NEW_LINE for i in range ( len ( word ) ) : NEW_LINE INDENT freq [ ord ( word [ i ] ) - 65 ] += 1 NEW_LINE if ( word [ i ] == ' A ' or word [ i ] == ' E ' or word [ i ] == ' I ' or word [ i ] == ' O ' or word [ i ] == ' U ' ) : NEW_LINE INDENT vowel += 1 NEW_LINE DEDENT else : NEW_LINE INDENT consonant += 1 NEW_LINE DEDENT DEDENT vowelArrange = factorial ( vowel ) NEW_LINE vowelArrange //= factorial ( freq [ 0 ] ) NEW_LINE vowelArrange //= factorial ( freq [ 4 ] ) NEW_LINE vowelArrange //= factorial ( freq [ 8 ] ) NEW_LINE vowelArrange //= factorial ( freq [ 14 ] ) NEW_LINE vowelArrange //= factorial ( freq [ 20 ] ) NEW_LINE consonantArrange = factorial ( consonant ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( i != 0 and i != 4 and i != 8 and i != 14 and i != 20 ) : NEW_LINE INDENT consonantArrange //= factorial ( freq [ i ] ) NEW_LINE DEDENT DEDENT total = vowelArrange * consonantArrange NEW_LINE return total NEW_LINE DEDENT word = " COMPUTER " NEW_LINE ans = count ( word ) NEW_LINE print ( ans ) NEW_LINE
Check if it is possible to create a palindrome string from given N | Function to check if a string is palindrome or not ; String that stores characters of s in reverse order ; Length of the string s ; String used to form substring using N ; Variable to store sum of digits of N ; Forming the substring by traversing N ; Appending the substr to str till it 's length becomes equal to sum ; Trimming the string str so that it 's length becomes equal to sum ; Driver code ; Calling function isPalindrome to check if str is Palindrome or not
def isPalindrome ( s ) : NEW_LINE INDENT s1 = " " NEW_LINE N = len ( s ) NEW_LINE i = ( N - 1 ) NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT s1 += s [ i ] NEW_LINE i = i - 1 NEW_LINE DEDENT if ( s == s1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def createString ( N ) : NEW_LINE INDENT s2 = " " NEW_LINE s = str ( N ) NEW_LINE letters = " abcdefghij " NEW_LINE sum = 0 NEW_LINE substr = " " NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT digit = int ( s [ i ] ) NEW_LINE substr += letters [ digit ] NEW_LINE sum += digit NEW_LINE DEDENT while ( len ( s2 ) <= sum ) : NEW_LINE INDENT s2 += substr NEW_LINE DEDENT s2 = s2 [ : sum ] NEW_LINE return isPalindrome ( s2 ) NEW_LINE DEDENT N = 61 ; NEW_LINE flag = createString ( N ) NEW_LINE if ( flag ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
Program to find the product of ASCII values of characters in a string | Function to find product of ASCII value of characters in string ; Traverse string to find the product ; Return the product ; Driver code
def productAscii ( str ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT prod = prod * ord ( str [ i ] ) NEW_LINE DEDENT return prod NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " GfG " NEW_LINE print ( productAscii ( str ) ) NEW_LINE DEDENT
Balance a string after removing extra brackets | Print balanced and remove extra brackets from string ; Maintain a count for opening brackets Traversing string ; check if opening bracket ; print str [ i ] and increment count by 1 ; check if closing bracket and count != 0 ; decrement count by 1 ; if str [ i ] not a closing brackets print it ; balanced brackets if opening brackets are more then closing brackets ; print remaining closing brackets ; Driver code
def balancedString ( str ) : NEW_LINE INDENT count , i = 0 , 0 NEW_LINE n = len ( str ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == ' ( ' ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE count += 1 NEW_LINE DEDENT elif ( str [ i ] == ' ) ' and count != 0 ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE count -= 1 NEW_LINE DEDENT elif ( str [ i ] != ' ) ' ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT DEDENT if ( count != 0 ) : NEW_LINE INDENT for i in range ( count ) : NEW_LINE INDENT print ( " ) " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " NEW_LINE INDENT balancedString ( str ) NEW_LINE DEDENT
Minimum operation require to make first and last character same | Python3 program to minimum operation require to make first and last character same ; Recursive function call ; Decrement ending index only ; Increment starting index only ; Increment starting index and decrement index ; Driver code ; Function call
import sys NEW_LINE MAX = sys . maxsize NEW_LINE def minOperation ( s , i , j , count ) : NEW_LINE INDENT if ( ( i >= len ( s ) and j < 0 ) or ( i == j ) ) : NEW_LINE INDENT return MAX NEW_LINE DEDENT if ( s [ i ] == s [ j ] ) : NEW_LINE INDENT return count NEW_LINE DEDENT if ( i >= len ( s ) ) : NEW_LINE INDENT return minOperation ( s , i , j - 1 , count + 1 ) NEW_LINE DEDENT elif ( j < 0 ) : NEW_LINE INDENT return minOperation ( s , i + 1 , j , count + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return min ( minOperation ( s , i , j - 1 , count + 1 ) , minOperation ( s , i + 1 , j , count + 1 ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " bacdefghipalop " NEW_LINE ans = minOperation ( s , 0 , len ( s ) - 1 , 0 ) NEW_LINE if ( ans == MAX ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans ) NEW_LINE DEDENT DEDENT
Count strings with consonants and vowels at alternate position | Function to find the count of strings ; Variable to store the final result ; Loop iterating through string ; If ' $ ' is present at the even position in the string ; ' sum ' is multiplied by 21 ; If ' $ ' is present at the odd position in the string ; ' sum ' is multiplied by 5 ; Driver code ; Let the string ' str ' be s$$e$ ; Print result
def countStrings ( s ) : NEW_LINE INDENT sum = 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( i % 2 == 0 and s [ i ] == ' $ ' ) : NEW_LINE INDENT sum *= 21 NEW_LINE DEDENT elif ( s [ i ] == ' $ ' ) : NEW_LINE INDENT sum *= 5 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " s $ $ e $ " NEW_LINE print ( countStrings ( str ) ) NEW_LINE DEDENT
Remove duplicates from a string in O ( 1 ) extra space | Function to remove duplicates ; keeps track of visited characters ; gets character value ; keeps track of length of resultant string ; check if Xth bit of counter is unset ; mark current character as visited ; Driver code
def removeDuplicatesFromString ( str2 ) : NEW_LINE INDENT counter = 0 ; NEW_LINE i = 0 ; NEW_LINE size = len ( str2 ) ; NEW_LINE str1 = list ( str2 ) ; NEW_LINE x = 0 ; NEW_LINE length = 0 ; NEW_LINE while ( i < size ) : NEW_LINE INDENT x = ord ( str1 [ i ] ) - 97 ; NEW_LINE if ( ( counter & ( 1 << x ) ) == 0 ) : NEW_LINE INDENT str1 [ length ] = chr ( 97 + x ) ; NEW_LINE counter = counter | ( 1 << x ) ; NEW_LINE length += 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT str2 = ' ' . join ( str1 ) ; NEW_LINE return str2 [ 0 : length ] ; NEW_LINE DEDENT str1 = " geeksforgeeks " ; NEW_LINE print ( removeDuplicatesFromString ( str1 ) ) ; NEW_LINE
Remove duplicates from a string in O ( 1 ) extra space | Method to remove duplicates ; Table to keep track of visited characters ; To keep track of end index of resultant string ; Driver code
def removeDuplicatesFromString ( string ) : NEW_LINE INDENT table = [ 0 for i in range ( 256 ) ] NEW_LINE endIndex = 0 NEW_LINE string = list ( string ) NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( table [ ord ( string [ i ] ) ] == 0 ) : NEW_LINE INDENT table [ ord ( string [ i ] ) ] = - 1 NEW_LINE string [ endIndex ] = string [ i ] NEW_LINE endIndex += 1 NEW_LINE DEDENT DEDENT ans = " " NEW_LINE for i in range ( endIndex ) : NEW_LINE ans += string [ i ] NEW_LINE return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT temp = " geeksforgeeks " NEW_LINE print ( removeDuplicatesFromString ( temp ) ) NEW_LINE DEDENT