Strings: Find the first repeated word in a string
Problem Statement: You are given a string, you need find the 1st repeated word in a string. Example Input: : "I am what I am" Output: I Here “I” is the first repeated word in the string. Solution The solution is very simple. Here we take a hashmap that has a word and its count. […]
Strings: Minimum number of flips to make binary string alternate
Problem Statement: You are given a binary string of 0s and 1s. You need to find the min number of flips required to make the sequence alternate by flipping some bits Example Input : “001” Output : 1 Here the minimum number of flips required are 1. If you flip the first bit from “0” […]
Strings: Longest Common Prefix
Problem Statement: You are given an array of strings. You need to find the longest common prefix in that array of strings. Example Input: ["flower","flow","flight"] Output: "fl" Solution We have many ways to solve this problem. In this tutorial we shall solve it by using sorting method. The solution is very simple. We sort the […]
Strings: Convert Roman Numerals to Decimal
Problem Statement: You are given a roman numeral, you need to convert into it’s corresponding decimal value. Example Input: IX Output: 9 Solution The solution is very simple. We need to know what each roman numeral maps to decimal. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 […]
Strings: Count the number of string in 2D character array
Problem Statement: You are given a string and a 2D char array. You need to count the number of times the string is there in the 2D array. The individual character can be present left to right, right to left, top to down or down to top. Example Input : a = { {B,B,H,B,B,B}, {O,B,E,B,B,B}, […]
Strings: Get the minimum number of inversions needed to make an expression balanced
Problem Statement: You are given a string consisting of “{” and “}”. You need to find the minimum number of inversions needed to balance the expression. Example Input: "}{" Output: 2 We need to change '}' to '{' and '{' to '}'. Then the expression becomes balanced '{}' Solution The solution is very simple. First […]
Strings: Convert a string into its mobile numeric keypad
Problem Statement: You are given a string, you need to convert that string into its equivalent mobile numeric keypad sequence. Example Input : HELLO WORLD Output : 4433555555666096667775553 Solution The solution is very simple. For each character, we will store the sequence of that number in an array. Then for every character subtract ASCII value […]
Strings: Word Break Problem
Problem Statement: You are given a word and series of dictionary. You need to check if the word can be broken down into multiple words that are present in the dictionary Example Dictionary { i, like, mobile, ice, cream, mango} Input: ilike Output: Yes The string can be separated as "i like". Solution The solution […]
Strings: Check balanced parenthesis
Problem Statement: You are given a set of parenthesis and you need to check if the given parenthesis are in correct exp. Example Input: “[()]{}[()()]()” Output: Balanced Input: “[(])” Output: Not Balanced Solution We will be able to solve this problem with the help of stacks. We will push the current character into the […]
Strings: Divide binary string into sub strings with equal number of 0s and 1s.
Problem Statement: You are given a string, you need to find the maximum count of substrings with equal number of 0s and 1s. Example Input: “0100110101” Output: 4 The substrings are “01”, “0011”, “01” and “01”. Solution The solution is very simple. We take a variable “count = 0” and traverse each character. Then we […]