Close

## Finding the edit distance between two strings

Given two strings as input, write a program to find the edit distance between them.  The edit distance is defined as the minimum number of modifications to do on one string to transform it into the other string. A modification can be inserting a character, deleting a character, or replacing an existing character. For example,…

## Checking if any anagram of a given string is palindrome or not

Given a string, how do we check if any anagram of it can be a palindrome? For example let us consider the string “AAC”. An anagram of it is “ACA” which is a palindrome. We have to write a method which takes a string and outputs true if we can form a palindrome from any…

## Reversing the words in the string

Given a string, how do we reverse the words inside the string? For example if the input string is “this is a test” the output should be “test a is this”. The algorithm is simple. This can be done in two steps. The first step is to reverse the entire string. In the second step…

## Removing a given character from the string

Given a string and a character, Write a function to remove all instances of the character and return the modified string. One easy method is to iterate through each character of the string, Once a restricted character is seen, we move all the characters appearing after it by one position. This approach involves a lot…

## Print a string in reverse using recursion

The following function is simply based on recursion mechanism. We write a function printReverse(). Initially we call this function by passing a pointer to the first character of the string. We keep calling this function recursively by incrementing the pointer until we reach the null character (End of the string). If we reach null, the…

## How to check if two strings are anagrams of each other

Anagram of a word is nothing but rearrangement of it’s letters to form another word.For example ‘listen’ is an anagram of ‘silent’ and ‘enlist’.We have to write a function to check if two given strings are anagrams are not. We can solve this problem in two different ways. The first way is to sort the…

## Detele characters from input string given in another string

Given two strings, How do we delete all the characters in the first string which are present in the second string? Find the character set of the string. Create a Boolean array of size equal to the character set size. If the character set is ASCII, the size is 256. This array will be indexed…

## Finding the first non repeating character in a string

Given a string as input, how do we find the first non-repeating character? We have to do this in linear time – O(n) and constant extra space- O(1) Assuming that the character set is ASCII, the number of characters possible are 256. This will differ if the string contains non-ASCII characters like Unicode. First we…

## How to find the frequecy of a word in a given sentence

Given some text and a word. How do we find the frequency of the word?In this post we will see a Java program which finds it with just a few lines of code using regular expression matching. Java provides utilities for matching the regular expressions using java.util.regex package. In our program we utilize two of…

## simple pattern matching

Pattern matching algorithms are very important in computer science. In this post we will discuss the simplest algorithm for pattern matching. Given some text and a pattern, we have to find where in the text, the pattern is found. In this brute-force algorithm, we start matching the pattern from the beginning of the text. If…