Skip to main content

Posts

Showing posts with the label strings

LeetCode Problem #794. Valid Tic-Tac-Toe State

Problem Statement: Validate whether given three set of combination leads to valid Tic-Tac-Toe  pattern or not. Given strings consists of 'x', 'o' or ' '. Assume that game starts from 'x'. So, first players1 puts 'x' in the game then player2 takes the turn and inserts 'o'. The game will continue until one of the players win. Our task is to find out whether given pattern is possible at any point of time of play. Return true if the given pattern is possible else return false. Example 1: Input: ['XXX', '   ', 'OOO'] Output:   false Explanation: The given pattern is not possible because once the player 1 put three X in a row which is in the first row game is over, player 2 doesn't get a chance to put thrid O. So, this pattern is not possible. Example 2: Input: ['XOX', 'OOX ', 'XO '] Output:   true Explanation: The given pattern is possible. Example 3: Input: ['OXX', 'XOX', ...

LeetCode Problem: 1028. Recover a Tree From Preorder Traversal

Problem Statement: Recover binary tree from its given preorder traversal string. The string is given in the format: Dashes followed by value. The number of dashes convey its depth and value refers to the node value. Example 1: Input: string = "1-2--3--4-5--6--7"; Output: [1,2,5,3,4,6,7] Example 2: Input: string = "1-2--3---4-5--6---7"; Output: [1,2,5,3,null, 6, null, 4, null, 7] Example 3: Input: string = "1-401--349---90--88"; Output: [1,401,null,349,88,90]. Approach to the solution: Calculate value and its depth Check if right child is present. If yes then move to right child then go to step 2 again until depth of the node is reached. Else move to left and then go to step 2 again until depth of the node is reached. Once the calculated depth is reached. If left child of the node is null, create a new node and assign left child of the node. Else create a new node and assign it to right child. Continue from step 1 again until end of the string is reached. S...

Leet Code: Problem #710 Random Pick with Blacklist

Given a blacklist  B containing unique integers from [0, N) , write a function to return a uniform random integer from [0, N) which is NOT  in B . Optimize it such that it minimizes the call to system’s Math.random() . Note: 1 <= N <= 1000000000 0 <= B.length < min(100000, N) [0, N)  does NOT include N. See interval notation . Example 1: Input: ["Solution","pick","pick","pick"] [[1,[]],[],[],[]] Output: [null,0,0,0] Example 2: Input: ["Solution","pick","pick","pick"] [[2,[]],[],[],[]] Output: [null,1,1,1] Example 3: Input: ["Solution","pick","pick","pick"] [[3,[1]],[],[],[]] Output: [null,0,0,2] Example 4: Input: ["Solution","pick","pick","pick"] [[4,[2]],[],[],[]] Output: [null,1,3,1] Explanation of Input Syntax: The input is two lists: the subroutines called and their argume...

Most frequently asked interview question #2 with solution

We have a string S of lowercase letters, and an integer array shifts . Call the shift of a letter, the next letter in the alphabet, (wrapping around so that 'z' becomes 'a' ). For example, shift('a') = 'b' , shift('t') = 'u' , and shift('z') = 'a' . Now for each shifts[i] = x , we want to shift the first i+1  letters of S , x times. Return the final string after all such shifts to S are applied. Solutions in C++: string shiftingLetters(string S, vector<int>& shifts) {             int sum = 0;             int iter = S.length() - 1;                 for(auto it = shifts.rbegin(); it != shifts.rend(); ++it, --iter){                         sum = sum + *it;  ...

Most frequently asked interview question #1 with solution

Given a string, find the length of the longest substring without repeating characters. Solution in C++:     int lengthOfLongestSubstring(string s) {                     int *char_index = (int*)malloc(sizeof(int) * 256);             int  max_len    = 1;             int  cur_len    = 1;                 if(s.length() == 0){             return 0;         }         //Initializing all the index values with -1 which means the character never appaered before         for(int iter = 0; iter < 256; ++ite...

String class in C++

In C++, another way of representing an array of characters is using Strings class. Using string class gives many advantages over Character array like: The size of the array has to be predefined if it is getting allocated statically(char arr[3]). In this case, there might be unused bytes which results in inefficient memory usage. But, in string class size won't be wasted as memory is allocated on need basis. There is a possibility of array decay in an array of characters(Array decay happens usually when an array of characters is passed as by the value of reference and the type or dimension is changed). Whereas in string class such problems don't arise as it uses objects. Characters array doesn't provide many inbuilt functions to handle operations on strings. Whereas in the string there are many inbuilt functions to do operations. Some of the built-in APIs provided by string class are: getline(cin, str): It is used to take input from the string. cin is also to take i...