Advanced Coding

🔤 Strings (Coding)

Day 2 | ⏱️ Part of 3-Hour Coding Block | TCS Interview Prep


📌 Subtopics

  • Anagrams: Check if two strings are anagrams, group anagrams
  • Palindromes: Check palindrome, longest palindromic substring
  • Frequency Counting: Character frequency, first non-repeating character
  • Pattern Matching: Naive approach, KMP algorithm basics

🌐 Learning Resources

ResourceLink
GFG — String Data Structuregeeksforgeeks.org
GFG — Anagram Checkgeeksforgeeks.org
GFG — Palindrome Checkgeeksforgeeks.org
GFG — Pattern Matching (KMP)geeksforgeeks.org
NeetCode — Stringsneetcode.io
LeetCode — String Problemsleetcode.com

✅ Must-Practice Problems (LeetCode)

ProblemDifficultyLink
Valid AnagramEasyleetcode.com/problems/valid-anagram
Valid PalindromeEasyleetcode.com/problems/valid-palindrome
Longest Palindromic SubstringMediumleetcode.com/problems/longest-palindromic-substring
First Unique CharacterEasyleetcode.com/problems/first-unique-character-in-a-string
Implement strStr()Easyleetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string
Longest Common PrefixEasyleetcode.com/problems/longest-common-prefix

📝 Key Patterns

# Check Anagram
from collections import Counter
def is_anagram(s, t):
    return Counter(s) == Counter(t)

# Check Palindrome
def is_palindrome(s):
    s = s.lower()
    return s == s[::-1]

# First Non-Repeating Character
def first_unique(s):
    freq = Counter(s)
    for i, ch in enumerate(s):
        if freq[ch] == 1:
            return i
    return -1

# Frequency Count
def char_frequency(s):
    freq = {}
    for ch in s:
        freq[ch] = freq.get(ch, 0) + 1
    return freq

  • Arrays and Hashing — Hashing patterns are core to string frequency counting and anagram checks
  • Dynamic Programming — Longest palindromic substring, edit distance, and regex matching are classic DP string problems
  • Patterns and Series — Alphabetic/numerical reasoning patterns and shift ciphers
  • Odd Man Out and Analogy — Letter analogies and word anagram identification
  • Verbal Ability — General grammar, context, and email writing (concepts of word groupings)