Bug: An elusive creature living in a program that makes it incorrect. The activity of "debugging", or removing bugs from a program, ends when people get tired of doing it, not when the bugs are removed.

Geeksforgeeks

It’s been a long time since I was trying to solve this problem. Whenever I tried I couldn’t solve it. I always miss some boundary conditions or something else. But today finally I solved it and it’s funny because approach is quite simple and I was trying code like building time machine 😛

Method 1: Brute Force approach – use two nested loops and make a pair and compare sum of them with given number. Its time complexity would be O(n^2).

Method 2: In this method we loop over the array only once. For each item of array, subtract this item from given number and if array item doesn’t exist in our list then add it otherwise print this item and subtraction of array item and given number. Its time complexity would be O(n) as we loop over only once and comparison are also less.

Method 1:Brute Force approach – use two nested loops and make a pair and compare sum of them with given number. Its time complexity would be O(n^2).

Method 2: In this method we loop over the array only once. For each item of array, subtract this item from given number and if array item doesn’t exist in our list then add it otherwise print this item and subtraction of array item and given number. Its time complexity would be O(n) as we loop over only once and comparison are also less.

Suppose you have a natural number say 20, you need to find out sum of its divisors. How will you start?

Input: number = 20

Output: 22

As we all know that a proper divisor can’t be greater than number/2. Also a prime divisor can’t be greater than Squar Root of the number. So now we have two approaches to compute above sum:

Method1 –Logic is, a proper divisor can’t be greater than number/2. This logic is preety simple but you have to loop till number/2. So suppose number is 100, then we have to iterate 50 times. Time complexity is O(n).

Method2 – Logic is, a prime divisor can’t be greater than Squar Root of the number. In this case our loop iteration will be considerabily less in compare to Method1.Time complexity is O(n).

Step1: Loop till condition i <= Squar root of number is true.Step2: Check for proper divisor.Step3: if it is proper divisor then add this number to our result.Step3.1: Check for the Quotient, if it is not same as divisor and divisor is not equal to 1 then add quotient to our result.

Given an array of n unique integers where each element in the array is in range [1, n]. The array is not necessarily sorted and has all distinct elements. The task is to find missing numbers if there is any.

Step1: First get a new filtered string from input string that has same characters as pattern.

1.1. Loop over all the characters of the input string.1.2. Check whether a input string’s character matches with any character of pattern.1.3. If there are any consecutive characters then add only one character to our filtered list. To do so, use a char type variable and match it with current variable, if matches then skip otherwise add current character to our filtered list.

Step2: Filtered list has only same number of characters as our pattern, if not then return FASLE, if yes then match all the characters. If there is any character that doesn’t match in the same sequence as pattern then return false otherwise return TRUE.

We are done now. Lets see the actual implementation of the above algorithm.

class StringPatternMatching
{
staticvoid Main(string[] args)
{
Console.WriteLine(IsStringFollowThePattern("engineers rock", "er"));
Console.WriteLine(IsStringFollowThePattern("engineers rock", "egr"));
Console.WriteLine(IsStringFollowThePattern("engineers rock", "gsr"));
Console.WriteLine(IsStringFollowThePattern("engineers rock", "eger"));
Console.ReadKey();
}
staticbool IsStringFollowThePattern(string input, string pattern)
{
var filteredInputCharacters = new List<char>();
char previousChar = default(char);
/// Loop over the input string to get only pattern matching
/// characters
foreach (var item in input)
{
/// Check for the pattern characters, if match then go
if (pattern.Contains(item))
{
/// If there are consecutive duplicate characters then add
/// only one, skip rest.If previous character is same as
/// current character then skip otherwise add it to our
/// filtered list.
if (previousChar != item)
{
filteredInputCharacters.Add(item);
previousChar = item;
}
}
}
/// Filtered list has only same number of characters as our pattern,
/// if not then return FASLE, if yes then match all the characters.
/// If there is any character that doesn't match in the same sequence
/// as pattern then return false otherwise return TRUE.
for (int i = 0; i < filteredInputCharacters.Count; ++i)
{
if (filteredInputCharacters[i] != pattern[j])
{
returnfalse;
}
}
returntrue;
}
}

The problem is to remove extra spaces from the string, from beginning of the string,
from end of the string and between words. But for special character like “.”, “,”, “?”
there should not be any space before them. Time complexity should be O(n).

Input: ” Hi , this is test string input. What do you think it’ll work ? ”
Output: “Hi, this is test string input. What do you think it’ll work?”