Minimum Deletions to make the occurrence of each character unique.

Objective: Given a string, your task is to delete the minimum number of characters in the string so that the count of each character is unique. Example: Input = “aaaaabbbbb” Output: 1 Explanation: In the input string, characters ‘a’ and ‘b’, both occur 5 times. You can delete either one ‘a’ or one ‘b’ to … Read more Minimum Deletions to make the occurrence of each character unique.

Compare two version numbers of a software

Given two versions of a software, write an algorithm to find out which version is the latest. Example: Version A: 1.1.0 Version B: 1.1.1 Given version B latest than version A Version A: 1.2.1 Version B: 1.2.1 Given versions are the same Version A: 2.2.0 Version B: 2.2 Given versions are the same Version A: … Read more Compare two version numbers of a software

Minimum number of times String A is repeated to such that B is substring of A

Given a string A consisting of n characters and a string B consisting of m characters, write a function that will return the number of times A must be stated such that B is a substring of the repeated A. If B can never be a substring, return -1. Example: A = ‘abcd’ B =’cdabcdab’ … Read more Minimum number of times String A is repeated to such that B is substring of A

Find Lexicographically smallest or largest substring of size k

Given a string and number k, write a program to find the lexicographically smallest substring of size k. Note: This problem can also be asked as find the lexicographically largest substring of size k Example: Input: “tutorial”, k = 2 Output: Smallest Lexicographically SubString: “al” Input: “tutorial”, k=3 Smallest Lexicographically SubString: “ial” Input: horizon, k=2 … Read more Find Lexicographically smallest or largest substring of size k

Largest word in dictionary by removing a few characters from the given string

Given a dictionary (list of words) and an input string K. Find the largest word in the dictionary which can be obtained by deleting a few characters in the input string K. Example: Dictionary: [tutorial, horizon, trial, zon] Input: taucdtorgibalbhsoariazaonzaqn Output: tutorial Dictionary: [tutorial, horizon, trial, zon], Input: attroialled Output: trial Approach: Please read – … Read more Largest word in dictionary by removing a few characters from the given string

Check if one string is a subsequence of another string.

Objective: Given two strings, write a program to check if any of the given string is a subsequence of another string. Example if given strings are A and B then EITHER A is subsequence of B OR B is subsequence of A, program should return true. Example: A = “horizon” B = “abhordizaonr” Output: true … Read more Check if one string is a subsequence of another string.

Print all steps to convert one string to another string

Objective: Given two strings, source string and target string, which are permutation or anagram of each other. You are allowed two swap only consecutive characters. Write an algorithm to print all the steps ( all the swaps) which will lead the conversion of the source string to the target string. NOTE: There could be multiple … Read more Print all steps to convert one string to another string

Lexicographically previous permutation With One swap

Objective: Given an array of integers (in particular order or permutation of a set of numbers), write an algorithm to find the lexicographically previous permutation of the given permutation with only one swap.  This problem can also be asked as “Given a permutation of numbers you need to find the next smaller premutation OR largest … Read more Lexicographically previous permutation With One swap

Hamming Distance between two given strings

Objective: Given two strings with equal lengths, write an algorithm to calculate the hamming distance between the strings.  Hamming Distance: Hamming distance between two strings is the number of positions at which the characters are different. Example: X = AABBCCDD, Y = AAAACCCC Hamming distance: 4 There are four positions at which bits are different … Read more Hamming Distance between two given strings

Find the maximum number present in a String

Given a string with alphabets and numbers. Write an algorithm to find the maximum number in the string. If any number is greater than equal to Max_Value of integer then return the Max_Value. Example: Input: “abcd 45, 54 and 50 are the numbers” Output: Maximum Number- 54 Input: aabb123ccdefgh54319hij Output: 54319 Input: 213123123and544455523412423423424234234234 Output: 2147483647 … Read more Find the maximum number present in a String

Convert Prefix to Postfix Expression

Objective: Given a Prefix expression, write an algorithm to convert it into Postfix expression. Example: Input: Prefix expression:  + A B  Output: Postfix expression: A B + Input: Prefix expression:  *-A/BC-/AKL Output: Postfix expression: ABC/-AK/L-* Approach: Use Stacks Algorithm: Iterate the given expression from right to left, one character at a time If the character … Read more Convert Prefix to Postfix Expression

Convert Postfix to Prefix Expression

Objective: Given a Postfix expression, write an algorithm to convert it into prefix expression. Example: Input: Postfix expression:  A B +  Output: Prefix expression- + A B Input: Postfix expression:  ABC/-AK/L-* Output: Infix expression: *-A/BC-/AKL Approach: Use Stack Algorithm: Iterate the given expression from left to right, one character at a time If the character … Read more Convert Postfix to Prefix Expression

Convert Integer to Roman

Objective: Given an Integer, write a program to convert it to Roman number. This problem can be stated as “Convert Decimal to Roman String” OR “Convert numeral value to Roman numeral”. Roman Number –   Letters used in Roman numerals and the corresponding numerical values are given in the table below. Rules: Roman numerals are … Read more Convert Integer to Roman

String to Integer (AtoI – ASCII to Integer) – Recursive Solution

Objective: Write a recursive function to implement atoi, which converts string to an integer. Rules for converting string to an integer. If the string is null or empty, Integer will be 0. The first character of the string may represent the sign of the integer. ‘+’ or ‘-‘ Discard the white spaces if necessary. If … Read more String to Integer (AtoI – ASCII to Integer) – Recursive Solution

Longest contiguous character in a given String – O(N) Solution

Objective: Given an input string, write an algorithm to find the longest contiguous character or in other words, find a maximum consecutive character in the string. Example: Input: “aaabbccccddbbaaa” Output: c, count = 4 Input: “bbbbb” Output: b, count=5 Approach: Naive Approach:  Naive Approach: Use  two nested loops The outer loop to fix it on … Read more Longest contiguous character in a given String – O(N) Solution