Array and String
Arrays & Strings in DSA (Java)
Master the most important topic for coding interviews and problem solving.
๐ Topics Covered
๐ Introduction
Arrays and strings are the foundation of most algorithms. Strings are essentially arrays of characters.
๐ก Key Insight: Mastering arrays = mastering problem solving.
⚡ Basic Operations
Access
arr[i]
Insertion & Deletion
- Requires shifting elements
- Time complexity: O(n)
Searching
- Linear Search → O(n)
- Binary Search → O(log n)
Sorting
- Bubble, Merge, Quick Sort
๐งช Common Problems
- Find duplicates
- Reverse array/string
- Check palindrome
- Two Sum problem
- Subarray sum
Reverse Array
int i = 0, j = arr.length - 1;
while (i < j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; j--;
}
Palindrome Check
while (l < r) {
if (s.charAt(l) != s.charAt(r)) return false;
l++; r--;
}
๐ Advanced Topics
- Sliding Window
- Two Pointer Technique
- Prefix Sum
- KMP Algorithm
๐ก Most interview problems use Sliding Window or Two Pointers.
☕ Java Implementation
Java provides arrays and String class for efficient manipulation and problem solving.
❓ FAQs
- Arrays are fixed size
- Strings are immutable in Java
- Edge cases are important
๐ฏ Conclusion
Arrays and strings form the base of almost all DSA problems and must be mastered thoroughly.