Strings

- Fundamentals -

String Reverse

1. Design an algorithm that rearranges the string characters from end to start.

  • Parameters - (String)
  • Output - String as per above problem statement
parameters \ cases Best CaseWorst CaseEdge Case
Parameters "hello" "abcdefghijklmnopqrstuvwxyz" "" (empty string)
Output"olleh" "zyxwvutsrqponmlkjihgfedcba" ""
ExplanationA normal word is reversed successfully. A long string with maximum distinct characters is reversed. An empty string remains the same after reversal.

String Palindrome

2. Design an algorithm that returns '1' if a given string is a palindrome else returns '0'.

  • parameters - (String)
  • output - 1 or 0 as per above problem statement
parameters \ cases Best CaseWorst CaseEdge Case
Parameters "racecar" "hello" "" (empty string)
Output1 0 1
ExplanationThe input is already a palindrome, so the function quickly returns 1. The input is not a palindrome, requiring full comparison before returning 0. An empty string is considered a palindrome, so the function should return 1.

First Non Repeating

3. Design an algorithm that returns the position (0- based index) of the first character that has no duplicates in the given string.

if no such character exist in the string the algorithm should return '-1'.

  • parameters - (String)
  • output - position or '-1' as per above problem statement
parameters \ cases Best CaseWorst CaseEdge Case
Parameters "abcdef" "aabbccddeeff" "aaaaaa"
Output0 -1 -1
ExplanationThe input is already a palindrome, so the function quickly returns 1. The first character 'a' is unique and appears only once, so the function returns 0 immediately. Every character appears more than once, so there is no non-repeating character, returning -1. All characters are identical, meaning no unique character exists, so return -1.

Anagram

4. Design an algorithm that returns '1' if the given two strings 'A' and 'B' are anagrams else returns '0'.

Anagram - Two strings are anagrams if they contain the same characters with the same frequency and have the same length.

  • parameters - (String A), (String B)
  • output - 1 or 0 as per above problem statement
parameters \ cases Best CaseWorst CaseEdge Case
Parameters Parameter: "listen", "silent" "abcdefghij", "jihgfedcba""a", "b"
OutputOutput: 1 10
Explanation Both strings have the same characters with the same frequency and are of the same length.Both strings contain the same characters with the same frequency but are in reverse order, making them valid anagrams.The strings have different characters, so they cannot be anagrams.

Non Repeating Substring

5. Design an algorithm that returns the longest substring that contains no repeating characters in string 's'

  • parameters - (String)
  • output - substring as per above problem statement
parameters \ cases Best CaseWorst CaseEdge Case
Parameters "abcdef""aaaaaa"""
Output"abcdef""a"""
ExplanationThe entire string "abcdef" has unique characters, so the longest substring is itself.All characters are the same, so the longest substring with unique characters is just "a".The input is an empty string, so there is no valid substring, returning "".

Count & Say

6. Design an algorithm that returns a 'count and say' string by processing the given string 's' by following these rules:

  • Identify consecutive groups of the same digit.
  • For each group, count the occurrences of the digit.
  • Construct the new string by appending the count followed by the digit for each group.
  • Return the processed "count and say" string.
  • parameters - (String)
  • output - String as per above problem statement
parameters \ cases Best CaseWorst CaseEdge Case
Parameters "1111""121212""" (Empty String)
Output"41""111211121112"""
ExplanationThe entire string consists of one repeating digit ('1'), so we count 4 occurrences and return "41".Every digit alternates, meaning each group contains only one digit, leading to a long output string where each digit is counted as 1. If the input string is empty, there are no digits to process, so the output remains empty.

Count & Say Sequence

7. Design an algorithm that returns the nth 'count and say' term.

The 'count and say' sequence is a series of strings where each term is generated by reading the previous term and describing its groups of consecutive digits.

The first term is '1', the second term is '11' (one 1), the third term is '21' (two 1s), the fourth term is '1211' (one 2, one 1), and so on.

parameters \ cases Best CaseWorst CaseEdge Case
Parameters n = 1n = 10n = 0
Output"1""13211311123113112211"""
ExplanationThe first term of the sequence is always "1" and requires no further processing.The 10th term is generated by iteratively applying the "count and say" transformation on the previous term. The sequence grows exponentially in length.If n = 0, there is no valid term in the sequence, so an empty string is returned (or could raise an error based on implementation).

Substring Search

8. Design an algorithm that returns the starting index (0-based index) of a given 'substring' present in the given 'main string'.

If the substring is not present in the mainstring, the algo should return -1.

  • parameters - (Main String), (Substring)
  • output - index or -1 as per above problem statement
parameters \ cases Best CaseWorst CaseEdge Case
Parameters "helloworld", "hello""aaaaaaaab", "aab""abcdef", "xyz"
Output06-1
ExplanationThe substring "hello" appears at the very beginning of the main string, making it an optimal scenario.The substring "aab" is found only at the second last position in "aaaaaaaab", requiring maximum comparisons before finding a match.The substring "xyz" is not present in "abcdef", so the function returns -1.

- Intermediate Problems -

Largest Palindrome

1. Design an algorithm that scans a string 's' and returns the longest consecutive substring that form a palindrome.

  • input - (String)
  • output - substring as per above problem statement
parameters \ casesBest CaseWorst CaseEdge Case
input"abacdfgdcaba""abcdefg""a"
output"aba" (or "aca")"a" (or any single character)"a"
explanationThe input string contains multiple palindromic substrings ("aba", "aca"), and the longest one is returned. This case runs efficiently.The input string has no repeated characters, so the longest palindrome is just a single character. The algorithm must scan the entire string, making it the worst case in terms of performance. The smallest input size (one character) is itself a palindrome, ensuring the algorithm correctly handles minimal input cases.

Group Anagrams

2. Design an algorithm that takes in array of strings and returns a list of group arrays where each group contains words that are anagram.

Anagram - Two strings are anagrams if they contain the same characters with the same frequency and have the same length.

  • input - (Array of Strings)
  • output - list of group arrays as per above problem statement
parameters \ casesBest CaseWorst CaseEdge Case
input["bat", "tab", "cat", "act"] ["abcdef", "fedcba", "ghijkl"] ["aaa", "aaa", "aaa"]
output[["bat", "tab"], ["cat", "act"]] [["abcdef", "fedcba"], ["ghijkl"]] [["aaa", "aaa", "aaa"]]
explanationEfficient sorting & grouping Computationally expensive Fastest case

Minimal Substring Match

3. Given a main string "S" and a target string "T". Design an algorithm that finds the smallest contiguous substring in main string that contains all characters of T (including duplicates), in any order.

If no such substring exists, returns an empty string.

  • input - (main string), (target string)
  • output - substring as per above problem statement
parameters \ casesBest CaseWorst CaseEdge Case
inputS = "abcdefghijk", T = "abc" S = "aa...aaabc", T = "abc" (Long S, T at the end) S = "abcdef", T = "xyz" (No match)
output"abc" "abc" "" (empty string)
explanationThe required substring is found at the beginning of S, requiring minimal iterations.The entire string must be scanned before finding the required substring at the end of S, leading to maximum iterations.No characters from T are in S, so no valid substring exists, returning an empty string.

Nested Parentheses

4. Given a string containing only three types of brackets:

  • Round Brackets: '(' and ')'
  • Curly Brackets: '{' and '}'
  • Square brackets: '[' and ']'

Design an algorithm that determine if the input string is valid, if valid returns 1 else 0.

A valid string satisfies the following conditions:

  • Every opening bracket has a corresponding closing bracket of the same type.
  • Brackets must close in the correct order (e.g."{[()]}" is valid but "{[(])}" is not).
  • A closing bracket should never appear before its corresponding opening bracket.
  • parameters - (String of parentheses)
  • output - 1 or 0 as per above problem statement
parameters \ casesBest CaseWorst CaseEdge Case
input "()""{[({[({[()]})]})]}" "])"
output1 1 0
explanationThe input has the smallest valid structure with matching parentheses.A deeply nested valid input with all opening and closing brackets properly ordered. The input starts with a closing bracket before an opening bracket, which is invalid.

String Compression

5. Design an algorithm that takes in a string 's' and compresses consecutive duplicate characters into a single instance of the character followed by its count and returns the compressed string.

If a character appears only once, it should remain unchanged e.g., "abc" - "abc" not "a1b1c1"

  • parameters - (String)
  • output - string as per above problem statement
parameters \ casesBest CaseWorst CaseEdge Case
input"abcdef" "aaaaaaaabbbbbbbcccccccc" "" (empty string)
output"abcdef" "a8b7c8" ""
explanationSince there are no consecutive duplicate characters, the string remains unchanged. The input has long consecutive repeating characters, resulting in maximum compression. An empty string should return an empty string without any modifications.

kth Permutation

6. Given a string 'S' and an integer 'k', Design an algorithm that returns the k th permutation of 'S' in lexicographical order.

If k is larger than the total number of unique permutations, return an empty string.

  • parameters - (string), (integer k)
  • output - permutation as per per above problem statement
Parameter \ Case Best Case Worst Case Edge Case
Parameters S = "abc", k = 1 S = "aaaaaaa", k = 1 S = "abc", k = 7
Output "abc" "aaaaaaa" "" (empty string)
Explanation - Small input. - First permutation directly. - No backtracking needed.- All characters are same. - Only 1 unique permutation exists. - Any k will result in same output.- k is larger than total unique permutations (which is 6 for "abc"). - Returns empty string.

Reverse Word by Word

7. Given a string containing words seperated by spaces. Design an algorithm that reverses the order of words.

  • parameters - (String)
  • output - string as per above problem statement
Parameter \ Case Best Case Worst Case Edge Case
Input String "hello" "a quick brown fox jumps over the lazy dog" "" (empty string) or string with multiple spaces " "
Output "hello" "dog lazy the over jumps fox brown quick a" "" (empty string)
Explanation Single word → no reversal needed Maximum number of words → requires full in-place reversal No words → should return empty string cleanly