Home Strings
Category:

Strings

View algorithms on Strings

  • Strings

    Longest Palindromic Substring

    by nikoo28
    4 minutes read

    Let us suppose that we are given a string ‘str’. You need to find out the longest palindrome as a substring. Note that the elements of the string should be contagious. Example 1:Input: “aaebabad”Output: “bab”Note: “aba” is also a valid answer Example 2:Input: “cbeereed”Output: “eeree” Terminology: Palindrome: A palindrome is a type of string that reads the same when reading left to right and right to left. An example of a palindrome can be “level”, “racecar”, “kayak” etc. Sub-string: A string formed formed using the characters of the string that…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    CamelCase matching

    by nikoo28
    3 minutes read

    Question: Given a set of queries and a pattern, we need to determine if some of those queries can be made from the given pattern by adding one or more lower case character. Output a boolean array where array[i] says true, if the query is possible, else false. Input: queries= {“StudyAlgorithms”, “StudyAlgorithmsTest”, “StanAlluring”, “SimulationAlteration”, “StopStayAlive”}pattern = “StAl” Output: {true, false, true, true, false} Let us try to understand the output.The pattern defined to us is St*Al* (if we see it in terms of a regex) StudyAlgorithms can be made as…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.Input: “abccccdd”Output: 7 In the above example, the longest palindrome in the given string is “dccaccd” whose length is 7. A palindrome consists of letters with equal partners, plus possibly a unique center (without a partner). The letter i from the left has its partner i from the right. For example in ‘abcba’, ‘aa’ and ‘bb’ are partners, and ‘c’ is a unique center. Imagine we…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given two strings, determine if they share a common sub-string. If they have a common sub-string, print YES else print NO. Input:studyalgorithmsalgosOutput: YES This is one of the easy problems but can get a little tricky, if we do not understand it completely. Let us try to understand the test caseString 1 = “studyalgorithms” String 2 = “algos”The sub-string “algo” is common between both the sub-strings and therefore the answer is “YES“. Let us try to take one more test case. String 1 = “hello” String 2 = “world”The…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [Hackerrank] – Palindrome Index

    by nikoo28
    3 minutes read

    Question: Given a string, identify the index of character to be removed to change the string into a palindrome. If the string cannot be converted to palindrome or is already a palindrome just return -1. Input: abckdeedcbaOutput: 3 (0 based indexing) To start off with the problem, we must first understand that there can be two possible ways to make the string a palindrome. If the given string is “bcbc”. If we remove the first ‘b’, the string becomes “cbc” which is a palindrome. If we remove the last ‘c’,…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Let us try to simplify the problem as much as we can. We are given with some of the terms. Weights:- According to the problem each character in the English alphabet has been assigned a weight. ‘a’ has been given a weight of 1, ‘b’ has a weight of 2, and so on. The weight of ‘z’ is 26. All the letters are assumed to be lowercase for simplicity. Weight of a string:- This is defined as the sum of all the characters in the string. Example if a string…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Pangrams

    by nikoo28
    2 minutes read

    Question: Given a string, identify if it is a pangram. Input: The quick brown fox jumps over the lazy little dog.Output: pangram To start with the problem statement we must first understand what is a pangram. A pangram is a string that contains all the characters in the English alphabet at-least once. This means that the string ‘the quick brown fox jumps over the lazy little dog’ will cover each of the letter from ‘a-z’. So how to identify if a string is a pangram?You need to keep a check…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [HackerRank] – Two Characters

    by nikoo28
    7 minutes read

    A string is said to be valid when it has only distinct characters and none of them repeat simultaneously. For example, if string ‘s two distinct characters are x and y, then valid examples could be xyxyx or yxyxy but not xxyy or xyyx.Question: Given a sample string, we need to determine what is the maximum length of valid string that can be made by deleting any of the characters.Input: beabeefeabOutput: 5 Explanation: Let us try to understand the output for the sample test case.If we delete e and f,…

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Next lexicographic string permutation

    by nikoo28
    4 minutes read

    Question: Given a string, find the next lexicographic permutation. Input: abcOutput: acb First of all let us try to understand what do you mean by next lexicographic string. To get a hold of the concept try to remember an English dictionary and how words are arranged in it. We start with the letter ‘a’, write all the words and then move to letter ‘b’ all the way to letter ‘z’. This is called as a lexicographic order.This is possible when we have all the letters in English alphabet available to…

    0 FacebookTwitterLinkedinWhatsappEmail
  • MiscStrings

    Common String Algorithms

    by nikoo28
    2 minutes read

    In this post, I would try to implement the following string-related algorithm questions. I would like to implement these using Java and try to make additional comments which I think that are useful for understanding the implementations of some type of data structures in Java programming language. Non Repeated Characters in String : Return the unique characters in a given letter. Anagram Strings : Given two strings, check if they’re anagrams or not. Two strings are anagrams if they are written using the same exact letters, ignoring space, punctuation and…

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More