SET 2
SET 2
Given a string s, sort it in decreasing order based on the frequency of the characters. The
frequency of a character is the number of times it appears in the string.
Test Case 1:
Input: s = "tree"
Output: "eert"
Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both
'r' and 't'. Therefore ""eetr"" is also a valid answer."
Test Case 2:
Input: s = "cccaaa"
Output: "aaaccc"
Test Case 3:
Input: s = "Aabb"
Output: "bbAa"
2.Password Strength Checker
Test Case 1:
Input: "P@ssw0rd!"
Expected Output: "Strong"
Explanation: This password meets all the criteria - it contains at least one uppercase
letter, one special character, and one number. It also has a length of 9 characters,
meeting the minimum length requirement for a strong password.
Test Case 2:
Input: "Moderate"
Expected Output: "Moderate"
Explanation: This password contains a special character and has a length of 9
characters, which is within the moderate range.
Test Case 3:
Input: "Ab12"
Expected Output: "Weak"
Explanation: This password has a length of only 4 characters, which is below the
minimum length requirement. Additionally, it does not contain any special characters.
3.Minimum additions to make a valid string
Given a string word to which you can insert letters "a", "b" or "c" anywhere and any
number of times, return the minimum number of letters that must be inserted so that
word becomes valid.
A string is called valid if it can be formed by concatenating the string "abc" several
times.
Test Case 1:
Output: 2
Explanation: Insert the letter "a" right before "b", and the letter "c" right next to "a" to
obtain the valid string "abc".
Test Case 2:
Output: 6
Explanation: Insert letters "b" and "c" next to each "a" to obtain the valid string
"abcabcabc".
Test Case 3:
Output: 0
Explanation: word is already valid. No modifications are needed.
4.String matching in an Array
Given an array of string words, return all strings in words that is a substring of another
word. You can return the answer in any order.
Test Case 1:
Output: ["as","hero"]
Explanation: "as" is substring of "mass" and "hero" is substring of "superhero".
["hero","as"] is also a valid answer.
Test Case 2:
Output: ["ce","car"]
Explanation: "ce", "car" are substring of "racecar”.
Test Case 3:
Output: []
Explanation: No string of words is substring of another string.
5.Shortest distance to target string in a circular array
You are given a 0-indexed circular string array words and a string target.
A circular array means that the array's end connects to the array's beginning.
Formally, the next element of words[i] is words[(i + 1) % n] and the previous element of
words[i] is words[(i - 1 + n) % n], where n is the length of words.
Starting from startIndex, you can move to either the next word or the previous word with
1 step at a time. Return the shortest distance needed to reach the string target. If the
string target does not exist in words, return -1
Test Case 1:
Output: 1
Explanation: We start from index 1 and can reach "hello" by - moving 3 units to the right
to reach index 4. - moving 2 units to the left to reach index 4. - moving 4 units to the
right to reach index 0. - moving 1 unit to the left to reach index 0. The shortest distance
to reach "hello" is 1
Test Case 2:
Output: -1
Explanation: Since "ate" does not exist in words, we return -1
6.Decrypt String from Alphabet to Integer Mapping
You are given a string s formed by digits and '#'. We want to map s to English lowercase
characters as follows:
The test cases are generated so that a unique mapping will always exist.
Test Case1:
Input: s = "10#11#12"
Output: "jkab"
Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
Test Case 2:
Input: s = "1326#"
Output: "acz"
Test Case 3:
Input: s = "33241"
Output: "ccbda"
7.Isomorphic Strings
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving
the order of characters. No two characters may map to the same character, but a
character may map to itself.
Constraints:
Test Case 1:
Output: true
Test Case 2:
Output: false
Test Case 3:
Output: true
8.String Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings. If
there is no common prefix, return an empty string "".
Test Case 1:
Output: "fl"
Explanation: The longest common prefix among the strings "flower", "flow", and "flight"
is "fl".
Test Case 2:
Output: ""
Explanation: There is no common prefix among the input strings
Test Case 3:
Output: "app"
Explanation: The longest common prefix among the strings "apple", "app", and
"application" is "app".