Posts

Showing posts from April, 2021

Strings Challenges | C++ Placement

Image
 String Challenges  Challenge 1  UpperCase-LowerCase interconversion  Given a string s with both uppercase and lowercase latin characters (‘a’ - ‘z’).  Your task is convert whole string into   1. Lower Case  2. Upper Case    Base idea: ‘a’ - ‘A’ = 32  1. Lowercase to UpperCase Approach  1. Iterate over the string s and if s[i] is a lower case character, then update s[i] -= 32    2. UpperCase to LowerCase Approach  1. Iterate over the string s and if s[i] is a upper case character, then update s[i] +=32    Code:    Challenge 2 Form the biggest number  of integers, our task is to form the biggest number out of those numbers in the string.    Approach:  Sort the string in descending order using inbuilt sort function.    Code     Challenge 3 Max Frequency  s of latin characters, your task is to output the character which has  maximum f...

Advanced Recursion Problems | C++ Placement

Image
Recursion - II   Permutation  To print all the permutations of a string.  Idea: for each character s[i] in the given string, we add a character in the ans string and then solve solve s.substr(0,i) + s.substr(i+1)    Sample Input:   ABC  Sample Output:  ABC  ACB  BAC  BCA  CAB  CBA    Time Complexity: O(N*2n )   Space Complexity: O(2n )      permutation(s, “”) will give the required answer   CountPaths  Find the number of ways to reach e from s.  Idea:  We have 6 ways to go forward (1,2,3,4,5,6).  At the starting point s,   Current answer = countPath(s+1,e) + countPath(s+2,e) + countPath(s+3,e) + countPath(s+4,e) + countPath(s+5,e) + countPath(s+6,e)  Time Complexity: O(2n)  Space Complexity: O(2n)    CountPathMaze  Given a 2D grid, find the number of ways to reach (n-1, n-1).  You can go to (i,j) from (i-1,j) and (...