Author: devangini123
π Rabin Karp The RabinβKarp algorithm is a string-searching algorithm used to find a pattern inside a larger text. Itβs especially useful when you need to search for multiple patterns or do repeated searches efficiently. It uses rolling hashing. text: a b c d b a c dβ¨ Pattern Hash: 7 (4+2+1) Add all the values of Hash. a = 1 b = 2 c = 3 d = 4 e = 5 f = 6 g = 7 h = 8 i = 9 j = 10 Now, I will proceed with the testing and create a window with…
π Problem Statement: Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b to be a substring of a after repeating it, return -1. Notice: string “abc” repeated 0 times is “”, repeated 1 time is “abc” and repeated 2 times is “abcabc”. Examples: Example 1: Input: a = “abcd”, b = “cdabcdab” Output: 3 Explanation: We return 3 because by repeating a three times “abcdabcdabcd”, b is a substring of it. Example 2: Input: a = “a”,…
π Problem Statement: Given two strings a and b, return the minimum number of times you should repeat string a so that string b is a substring of it. If it is impossible for b to be a substring of a after repeating it, return -1. Notice: string “abc” repeated 0 times is “”, repeated 1 time is “abc” and repeated 2 times is “abcabc”. Examples: Example 1: Input: a = “abcd”, b = “cdabcdab” Output: 3 Explanation: We return 3 because by repeating a three times “abcdabcdabcd”, b is a substring of it. Example 2: Input: a = “a”,…
π Problem Statement: Given a string s, rearrange the characters of s so that any two adjacent characters are not the same. Return any possible rearrangement of s or return “” if not possible. Examples: Example 1: Input: s = “aab” Output: “aba” Example 2: Input: s = “aaab” Output: “” Constraints 1 Math.ceil(n / 2)) { return “”; } // Sort characters by frequency (descending) let chars = Object.keys(freq).sort((a, b) => freq[b] – freq[a]); let result = new Array(n); let i = 0; // Fill even indices first, then odd for (let ch of chars) { let count =…
π Problem Statement: The count-and-say sequence is a sequence of digit strings defined by the recursive formula: countAndSay(1) = “1” countAndSay(n) is the run-length encoding of countAndSay(n – 1). Run-length encoding (RLE) is a string compression method that works by replacing consecutive identical characters (repeated 2 or more times) with the concatenation of the character and the number marking the count of the characters (length of the run). For example, to compress the string “3322251” we replace “33” with “23”, replace “222” with “32”, replace “5” with “15” and replace “1” with “11”. Thus the compressed string becomes “23321511”. Given…
π Problem Statement: Given an encoded string, return its decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer. You may assume that the input string is always valid; there are no extra white spaces, square brackets are well-formed, etc. Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there will not be input like 3a or 2[4]. The test cases are generated so…
π Problem Statement: The beauty of a string is the difference in frequencies between the most frequent and least frequent characters. For example, the beauty of “abaacc” is 3 – 1 = 2. Given a string s, return the sum of beauty of all of its substrings. Examples: Example 1: Input: s = “aabcb” Output: 5 Explanation: The substrings with non-zero beauty are [“aab”,”aabc”,”aabcb”,”abcb”,”bcb”], each with beauty equal to 1. Example 2: Input: s = “aabcbaa” Output: 17 Constraints 1 int: beauty_sum = 0 for i in range(len(s)): freq = [0] * 26 for j in range(i, len(s)): index =…
π Problem Statement: Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string of the words in reverse order concatenated by a single space. Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces. Examples: Example 1: Input: s = “the sky is blue” Output: “blue is sky the” Example 2:…
π Problem Statement: A parentheses string is valid if and only if: It is the empty string, It can be written as AB (A concatenated with B), where A and B are valid strings, or It can be written as (A), where A is a valid string. You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string. For example, if s = “()))”, you can insert an opening parenthesis to be “(()))” or a closing parenthesis to be “())))”. Return the minimum number of moves required to make s…
π Problem Statement: You are given k identical eggs and you have access to a building with n floors labeled from 1 to n. You know that there exists a floor f where 0
Contact Us
Subscribe to Stay Updated
Stay ahead in the world of tech with our exclusive newsletter! Subscribe now for regular updates on the latest trends, valuable coding resources, and tips to boost your frontend development skills.
