Eliminate the pairs (two same chars adjacent to each other) in string

Problem: You are given a string. You have to eliminate the pairs (two same chars adjacent to each other). eg. input string RGBBGBGR –> RGGBGR–> RBGR (output) Solution: We should check if we have character pair then cancel it. and then check for next character and previous character. Keep canceling the character until we either Read More →

Length of Longest substring without repeating characters

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. For “bbbbb” the longest substring is “b”, with the length of 1. Solution: When you have found a repeated character (let’s say at index j), Read More →

Remove characters from input string that are present in mask string

Write a C program that takes two strings as arguments and removes the characters from first string which are present in second string called mask string. ex: str1 “crazy for code” str2 “cod” Result: “razy e” Steps: 1) Get count array from mask string which stores count of chars from mask string. 2) Check for Read More →

Find first non repeating character in a string

You are given a string, find its first non-repeating character? Algorithm: 1) Scan the string from left to right and construct the count array. 2) Again, scan the string from left to right and check for count of each character, if you find an element who’s count is 1, return it. Time complexity: O(N)

Write a function to check whether two strings are anagram of each other.

Input: s – “abcde”, t – “abced” Output = true Input  s – “abcde”, t – “abcfed” Output – false Below are two solutions to this problem. Method 1: Sort both the strings and then compare the strings. Time Complexity is O(nlogn). Method 2: Check if the two strings have same count for each character. Read More →

Remove duplicate characters in a string

Given a string, Write a program to remove duplcate characters from the string. Input String : crazyforcode Output String : crazyfode Algorithm : 1. For each character, check if it is duplicate of already found characters. 2. Skip duplicate characters and update the non duplicate characters. Method 1 Using Extra Space. Time Complexity : O(N) Read More →

C program to reverse a string

For example if a user enters a string “crazyforcode” then on reversing the string will be “edocrofyzarc “. We show you two different methods to reverse string the first one we make our own function to reverse string using pointers and in second, reverse string using recursion. Method 2 : Using recursion In recursion method Read More →

Print letters followed by their frequency (Run Length Encoding)

Given a string ,Write a program to print letter followed by it’s frequency.This is also knows as Run Length Encoding. Example: Input aaabcc Output a3b1c2 Algorithm: 1.Pick the first character from the string and print it. 2.Count the number of subsequent occurrences of the picked character and then print the count. 3.Pick the next character Read More →

Reverse a string word by word,(in place)

Write a Program to reverse the words in a string. Example Input : Hello! How are you Output: you are How Hello! The words are reversed, but the letters are still in order (within the word). Algorithm: i)Reverse the whole string. You will get “uoy era woH !olleH” ii)Then reverse each word. Now it will Read More →