Longest Common Prefix

Write a program to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string ” “. Example 1: Input: [“geeksblood”, “geeks”, “geek”] Output: “geek” Example 2: Input: [“abc”,”xyz”,”pqr”] Output: “” Explanation: There is no common prefix among the input strings. Implementation: class Program { […]

Read More

Balanced Brackets

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered […]

Read More

Find all permutations of a String

Given a string, find all the permutations of the string. For example: Input String: abc Output: {bca, acb, abc, cba, bac, cab} Program: class Program { /*Function to print permutations of string This function takes three parameters: 1. String 2. Starting index of the string 3. Ending index of the string.*/ private static void permute(String […]

Read More

Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note: you cannot sell a stock […]

Read More

Maximum Subarray

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 // C# program to print largest contiguous array sum class Program { static int maxSubArraySum(int[] a) { int size = a.Length; int max_so_far = int.MinValue, max_ending_here = […]

Read More

Anagram

Two strings are said to be anagrams, if one string can be obtained by rearranging the letters of another. Examples: dog, god abac, baac anagram, nagaram silent, listen class Anagram { public static void IsAnagram(string word1, string word2) { bool flag = true; if (word1.Length != word1.Length) { flag = false; } var aFrequency = […]

Read More

Array vs Linked List

Arrays and Linked Lists both are linear data structures, but they both have some advantages and disadvantages over each other. One advantage of the linked list is that elements can be added to it indefinitely, while an array will eventually get filled or have to be resized (a costly operation that isn’t always possible). Elements […]

Read More