Deep Dive into Strings: The Unsung Hero of DSA (C++ Edition)
Blog-1 | DSA Expert Series
Strings are one of the most important data structures in competitive programming and interviews. Mastering them in C++ gives you a massive edge.
What is a String in C++?
In C++, std::string is a dynamic, mutable character sequence (unlike Java/Python where strings are immutable).
#include <bits/stdc++.h>
using namespace std;
int main() {
string s = "hello";
s += " world"; // mutable - efficient
cout << s << endl; // hello world
cout << s.length() << endl; // 11 (O(1))
cout << s[0] << endl; // h β O(1) access
}
Key Operations & Best Practices (C++)
| Operation | Recommended Way | Time Complexity |
|---|---|---|
| Concatenation |
+= or reserve()
|
Amortized O(1) per op |
| Build large string |
reserve() + +=
|
O(n) total |
| Reverse | reverse(s.begin(), s.end()) |
O(n) |
| Substring | s.substr(pos, len) |
O(len) |
Pro Tip: Always use s.reserve(n) when you know the final size to avoid reallocations.
Must-Know Techniques with C++ Code
1. Two Pointers - Reverse String (In-place)
void reverseString(string &s) {
int left = 0, right = s.length() - 1;
while (left < right) {
swap(s[left++], s[right--]);
}
}
2. Check Palindrome (Ignoring non-alphanumeric)
bool isPalindrome(string s) {
int left = 0, right = s.length() - 1;
while (left < right) {
while (left < right && !isalnum(s[left])) left++;
while (left < right && !isalnum(s[right])) right--;
if (tolower(s[left++]) != tolower(s[right--]))
return false;
}
return true;
}
3. Longest Substring Without Repeating Characters (Sliding Window)
int lengthOfLongestSubstring(string s) {
unordered_map<char, int> mp;
int left = 0, maxLen = 0;
for (int right = 0; right < s.length(); right++) {
if (mp.count(s[right]) && mp[s[right]] >= left) {
left = mp[s[right]] + 1;
}
mp[s[right]] = right;
maxLen = max(maxLen, right - left + 1);
}
return maxLen;
}
4. Group Anagrams (Using Frequency Array - Fastest)
string getKey(const string& str) {
vector<int> count(26, 0);
for (char c : str) {
if (islower(c)) count[c - 'a']++;
}
string key = "";
for (int freq : count) key += to_string(freq) + "#";
return key;
}
Advanced: Rolling Hash (Rabin-Karp) in C++
long long rollingHash(const string& s) {
const long long MOD = 1e9 + 7;
const long long base = 31;
long long hash = 0, power = 1;
for (char c : s) {
hash = (hash + (c - 'a' + 1) * power) % MOD;
power = (power * base) % MOD;
}
return hash;
}
Pro Tips for C++ Developers
- Prefer
std::stringoverchar[]. - Use
s.reserve()before heavy concatenations. -
string_view(C++17) for read-only views β very fast, zero copy. - For very large input, use fast input:
ios::sync_with_stdio(false); cin.tie(NULL);
Practice Path
Easy: Reverse String, Valid Palindrome, Valid Anagram
Medium: Longest Substring Without Repeating, Group Anagrams, Minimum Window Substring
Hard: Longest Palindromic Substring, Distinct Subsequences, Wildcard Matching
Strings may look easy, but deep mastery (especially in C++) separates good coders from experts.
Next Blog: Deep Dive into Tries & Suffix Structures in C++.
Whatβs your favorite string problem? Comment below π
Happy Coding!
About the Author
Rihanul Islam
Competitive Programmer | DSA Enthusiast | Building strong fundamentals in Data Structures and Algorithms.
Top comments (0)