Welcome back, coding explorer!
Today’s DSA Bite takes you into the world of Strings - one of the most fundamental and interview-heavy data structures. Whether you’re reversing names, validating palindromes, or parsing user inputs, strings are everywhere.
We’ll break it all down - from basics to advanced patterns - just like we did for arrays.
Part 1: What is a String?
A string is a sequence of characters - letters, digits, symbols - enclosed in quotes.
Think of it like a necklace of characters, where each bead (character) has an index.
Example
name = "Fahim"
print(name[0]) # F
let name = "Fahim";
console.log(name[0]); // F
Key String Terms
Term | Description | Example |
|---|---|---|
Character | A single symbol |
|
Index | Position of a character (starts at 0) |
|
Immutable | Strings can’t be changed directly | You must create a new one |
Length | Number of characters |
|
Part 2: Common String Operations
1️⃣ Accessing Characters
s = "hello"
print(s[1]) # e
let s = "hello";
console.log(s[1]); // e
2️⃣ Concatenation
greet = "Hi " + "there"
print(greet) # Hi there
let greet = "Hi " + "there";
console.log(greet); // Hi there
3️⃣ Substring
s = "CodeAlignr"
print(s[0:4]) # Code
let s = "CodeAlignr";
console.log(s.substring(0,4)); // Code
4️⃣ Searching
if "Align" in s:
print("Found!")
if (s.includes("Align")) console.log("Found!");
5️⃣ Replace & Split
text = "I love Python"
print(text.replace("Python", "AI"))
print(text.split(" "))
let text = "I love JavaScript";
console.log(text.replace("JavaScript", "AI"));
console.log(text.split(" "));
Part 3: Common Interview Patterns
1️⃣ Reverse a String
s = "hello"
print(s[::-1]) # olleh
let s = "hello";
console.log(s.split('').reverse().join('')); // olleh
2️⃣ Check Palindrome
s = "madam"
print(s == s[::-1]) # True
let s = "madam";
console.log(s === s.split('').reverse().join('')); // true
3️⃣ Count Vowels
s = "developer"
count = sum(1 for c in s if c in "aeiou")
print(count)
let s = "developer";
let count = [...s].filter(c => "aeiou".includes(c)).length;
console.log(count);
4️⃣ Frequency of Characters
from collections import Counter
print(Counter("banana"))
let s = "banana";
let freq = {};
for (let ch of s) freq[ch] = (freq[ch] || 0) + 1;
console.log(freq);
Part 4: Advanced Patterns
Two Pointers – Remove Duplicates or Check Palindrome Efficiently
s = "level"
left, right = 0, len(s)-1
is_pal = True
while left < right:
if s[left] != s[right]:
is_pal = False
break
left += 1
right -= 1
print(is_pal)
let s = "level";
let left = 0, right = s.length - 1, isPal = true;
while (left < right) {
if (s[left] !== s[right]) { isPal = false; break; }
left++; right--;
}
console.log(isPal);
Sliding Window – Longest Substring Without Repeating Characters
s = "abcabcbb"
left = 0
seen = set()
max_len = 0
for right in range(len(s)):
while s[right] in seen:
seen.remove(s[left])
left += 1
seen.add(s[right])
max_len = max(max_len, right - left + 1)
print(max_len)
let s = "abcabcbb";
let left = 0, seen = new Set(), maxLen = 0;
for (let right = 0; right < s.length; right++) {
while (seen.has(s[right])) {
seen.delete(s[left]);
left++;
}
seen.add(s[right]);
maxLen = Math.max(maxLen, right - left + 1);
}
console.log(maxLen);
Anagram Check
s1, s2 = "listen", "silent"
print(sorted(s1) == sorted(s2))
let s1 = "listen", s2 = "silent";
console.log([...s1].sort().join('') === [...s2].sort().join(''));
Part 5: Mini Challenges
1️⃣ Reverse words in a sentence
Input: "I love Python" → Output: "Python love I"
2️⃣ Find the first non-repeating character
Input: "aabbcd" → Output: "c"
3️⃣ Check if two strings are anagrams.
Input: "angel", "glean" → Output: True
4️⃣ Find the longest common prefix.
Input: ["flower", "flow", "flight"] → Output: "fl"
Expert Challenge – String Compression
Compress a string like "aaabbcc" → "a3b2c2"
s = "aaabbcc"
res, count = "", 1
for i in range(1, len(s)):
if s[i] == s[i-1]:
count += 1
else:
res += s[i-1] + str(count)
count = 1
res += s[-1] + str(count)
print(res)
let s = "aaabbcc";
let res = "", count = 1;
for (let i = 1; i < s.length; i++) {
if (s[i] === s[i - 1]) count++;
else { res += s[i - 1] + count; count = 1; }
}
res += s[s.length - 1] + count;
console.log(res);
🤖 AI Tip
Ask AI:
“Explain and generate problems using Two Pointer and Sliding Window techniques on strings.”
Use it to practice substring, palindrome, and frequency problems efficiently.
✅ Sign-off
That’s your Strings Bite – Beginner to Expert! 🎉
You’ve explored everything from basic manipulation to advanced string algorithms.
Next up: Linked Lists - where data gets dynamic and connected!