0% found this document useful (0 votes)
3 views

SET 2

The document contains multiple programming challenges, including sorting characters by frequency, checking password strength, validating strings, finding substrings in an array, calculating distances in a circular array, decrypting strings, checking isomorphic strings, and finding the longest common prefix. Each challenge is accompanied by test cases that illustrate the expected input and output. The problems cover various aspects of string manipulation and algorithm design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

SET 2

The document contains multiple programming challenges, including sorting characters by frequency, checking password strength, validating strings, finding substrings in an array, calculating distances in a circular array, decrypting strings, checking isomorphic strings, and finding the longest common prefix. Each challenge is accompanied by test cases that illustrate the expected input and output. The problems cover various aspects of string manipulation and algorithm design.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Arrange in desending order

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

Cather is assigned to create a Password Strength Checker program to evaluate the


strength of user-provided passwords. The program should assess the password based
on various criteria and provide a strength score or category to help users choose secure
passwords.
Constraint:
1. Must contain at least one Uppercase, one Special character, one number.
2. If a minimum of 8 or more characters with special characters and numeric is
considered as “Strong”.
3. With size of 5 or more than 5 characters with only alphabets is considered as
“Moderate”
4. Password with 4 or less than four characters or continuous numeric digits is
considered as “Weak”.

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:

Input: word = "b"

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:

Input: word = "aaa"

Output: 6
Explanation: Insert letters "b" and "c" next to each "a" to obtain the valid string
"abcabcabc".

Test Case 3:

Input: word = "abc"

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.

A substring is a contiguous sequence of characters within a string


Constraints: 1 <= words.length <= 100 1 <= words[i].length <= 30 words[i] contains only
lowercase. English letters. All the strings of words are unique.

Test Case 1:

Input: words = ["mass","as","hero","superhero"]

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:

Input: words = ["racecar","ce","car"]

Output: ["ce","car"]
Explanation: "ce", "car" are substring of "racecar”.

Test Case 3:

Input: words = ["blue","green","rp"]

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:

Input: words = ["hello","i","am","program ","hello"], target = "hello", startIndex = 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:

Input: words = ["i","eat"," program "], target = "ate", startIndex = 0

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:

Characters ('a' to 'i') are represented by ('1' to '9') respectively.


Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.
Return the string formed after mapping.

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

Given two strings s and t, determine if they are isomorphic.

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:

1 <= s.length <= 5 * 104


t.length == s.length
s and t consist of any valid ascii character.

Test Case 1:

Input: s = "egg", t = "add"

Output: true

Test Case 2:

Input: s = "foo", t = "bar"

Output: false

Test Case 3:

Input: s = "paper", t = "title"

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:

Input: strs = ["flower", "flow", "flight"]

Output: "fl"
Explanation: The longest common prefix among the strings "flower", "flow", and "flight"
is "fl".

Test Case 2:

Input: strs = ["dog", "racecar", "car"]

Output: ""
Explanation: There is no common prefix among the input strings

Test Case 3:

Input: strs = ["apple", "app", "application"]

Output: "app"
Explanation: The longest common prefix among the strings "apple", "app", and
"application" is "app".

You might also like