View algorithms on Strings
Given some input strings, we need to find the longest common prefix or the longest starting sequence of characters that is common
View algorithms on Strings
A camel case is a special string representation in which the first word starts with a small letter, and all other words start with an upper case character. Given such a string, find the total number of words
Let us try to understand this problem statement first. We have some strings that needs to be super reduced in size. The string consists of english alphabets from a to z, and they can be duplicated at some places. If we see two adjacent characters that are same, they can be eliminated. Following this pattern we have to return a super compressed/reduced string. Let us look at some sample test cases: Input: aabcccddOutput: “bc”Explanation:aabcccdd -> bcccdd -> bcdd -> bc(we eliminate two same adjacent characters at each step) Input: abbaOutput:…
Question: Given a two strings, find the minimum number of characters that must be deleted to make them anagrams. Input: str1 = “a b c”, str2 = “a m n o p”Output: 6 Let us try to understand this problem statement and its test case first. We have two strings of english alphabet which may or may not have the same length. Anagrams are two words with same characters and frequencies. It is not necessary that the order of the characters is same. We have to determine, how many total…
Question: Given a string, count the number of times the letter ‘a’ is repeated. Input: str = “abcac”, n= 10 Output: 4 Let us try to understand this problem statement and its test case first. You have a string of lowercase English letters, that is repeated infinitely many times. Along with it, you also have a number ‘n’. You need to find out how many times the letter a appears in the first n characters of this infinite string. Let us look at a sample test case. Upon looking at…
Given an array of strings, group all the anagrams together. This post explores 2 methods to solve this problem. You can create groups by sorting, or by categorizing using the frequency of characters.
Question: Given a string, Sherlock considers it valid if all the characters in the string occur the same number of time. However, a string is also valid if the frequencies are same after removing any one character. Example 1:Input: str = “aabbcd”Output: NO Example 2:Input: str = “abcc”Output: YES Problem Statement: Let us try to simplify the problem statement first. Sherlock needs to verify if a given string is valid. A string is valid under 2 conditions: All characters of the string appear the same number of times. If you…
Question: Given two strings, determine if they share a common sub-string. Input: “a” and “art” Output: YES Problem Statement Let me try to simplify this problem statement first. You are given two strings, and you need to find out if they share a common sub-string or not. If you can find a common sub-string, output “YES”, else output “NO”. Note that you don’t have to find out what is the common sub-string. Another important aspect of this problem is that the sub-string can be as small as 1 character. A…
Given two strings str1 and str2. Write a function to determine if str1 is an anagram of str2. Example 1:str1 = “listen”, str2 = “silent”Output = True Example 2:str1 = “mississippi”, str2 = “mips”Output = False Terminology: Anagram: Two words or phrases are said to be anagrams of each other if they can be formed by re-shuffling of characters in one of them. Problem Statement: In the given problem we have 2 strings as input. They may be single words or phrases. Return true if they anagrams of each other,…
You are given a string with some repeating characters and some unique characters. Find the first unique character and return its index. Example 1:Input: studyAlgorithmsOutput: 2 Example 2:Input: iLoveToCodeOutput: 0 Problem Statement: I want to simplify the problem statement first before we begin to solve it. An input string consists of some uppercase and lowercase characters which may or may not be repeated. Out of all the unique characters, you need to return the characters that occurs first in the string. In the first example studyAlgorithms, s, and t are…