STRINGS PROBLEM-SOLVING TRICKS
Essential String Manipulation Tricks
| # | Trick | Description |
|---|---|---|
| 1 | Two-pointer on strings | Use two indices (start and end) to compare or manipulate string efficiently |
| 2 | Character frequency counting | Use an int freq[256] = {0} to count each character using ASCII values |
| 3 | In-place string reversal | Swap characters using two pointers, no extra string needed |
| 4 | Removing duplicates | Mark visited chars with a boolean array or use slow-fast pointers |
| 5 | String tokenization | Use strtok() to split string based on delimiters like spaces, commas |
| 6 | String rotation check | Concatenate string with itself and check if other string is a substring |
| 7 | Substring search (Brute/Optimized) | Use nested loops for brute, or implement KMP/Rabin-Karp |
| 8 | String to number / number to string | Use atoi(), itoa() or manual conversion via loop |
| 9 | Prefix/Suffix matching | Compare beginning or end characters using strncmp() or manual loop |
| 10 | Reversing words in string | First reverse entire string, then reverse each word |
| 11 | Palindrome subsequence / substring | Check longest palindromic subsequence or substring |
| 12 | Sliding window on strings | Maintain frequency window to find smallest/largest substrings |
| 13 | Lexicographic operations | Use strcmp(), sorting, etc., to compare alphabetically |
| 14 | Edit distance / DP on strings | Use 2D DP table to compute min operations to convert strings |
| 15 | Bit masking for lowercase letters | Use 26-bit integer to track lowercase letters efficiently |
Implementation Examples
Two-pointer Example
// Palindrome check using two pointers
int isPalindrome(char *s) {
int left = 0, right = strlen(s) - 1;
while (left < right) {
if (s[left++] != s[right--])
return 0;
}
return 1;
}
Frequency Counting
// Count character frequencies
void countChars(char *s) {
int freq[256] = {0};
for (int i = 0; s[i]; i++) {
freq[(int)s[i]]++;
}
// Print frequencies
for (int i = 0; i < 256; i++) {
if (freq[i]) printf("%c: %d\n", i, freq[i]);
}
}
In-place Reversal
// Reverse string in-place
void reverseString(char *s) {
int n = strlen(s);
for (int i = 0, j = n-1; i < j; i++, j--) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
Sliding Window
// Longest substring without repeating chars
int lengthOfLongestSubstring(char *s) {
int freq[256] = {0}, left = 0, max_len = 0;
for (int right = 0; s[right]; right++) {
freq[s[right]]++;
while (freq[s[right]] > 1) {
freq[s[left++]]--;
}
max_len = fmax(max_len, right-left+1);
}
return max_len;
}
Pro Tips for String Manipulation
Always remember: C strings are null-terminated! Forgetting the null terminator is a common source of bugs.
Safety Practices
- Always check string lengths before operations
- Use bounded functions (strncpy, snprintf)
- Validate input strings before processing
- Initialize string buffers properly
Performance Tips
- Pointer arithmetic is often faster than array indexing
- Avoid unnecessary strlen() calls in loops
- Precompute values when possible
- Choose algorithms based on problem constraints
Related Resources
Related String Resources