String Functions
TOP Java String Methods for DSA
Method What it does Example
length() Returns length of the string "abc".length() → 3
charAt(index) Returns character at given index "hello".charAt(1) → 'e'
substring(start, end) Returns substring from start to end-1 "hello".substring(1, 4) → "ell"
Checks if two strings are equal (case-
equals(str) "abc".equals("abc") → true
sensitive)
"abc".equalsIgnoreCase("ABC") →
equalsIgnoreCase(str) Compares strings ignoring case
true
contains(str) Checks if string contains substring "hello".contains("ell") → true
indexOf(ch) Finds index of first occurrence "hello".indexOf('l') → 2
lastIndexOf(ch) Finds index of last occurrence "hello".lastIndexOf('l') → 3
toCharArray() Converts string to a character array "abc".toCharArray() → ['a','b','c']
Splits string into words using space or
split(" ") "I love Java".split(" ")
regex
replace(old, new) Replaces all old chars with new ones "abc".replace('a','z') → "zbc"
startsWith(str) Checks if string starts with given str "hello".startsWith("he") → true
endsWith(str) Checks if string ends with given str "hello".endsWith("lo") → true
trim() Removes leading and trailing spaces " hello ".trim() → "hello"
toLowerCase() Converts entire string to lowercase "HELLO".toLowerCase() → "hello"
toUpperCase() Converts entire string to uppercase "hello".toUpperCase() → "HELLO"
isEmpty() Checks if string is empty (length == 0) "".isEmpty() → true
Compares two strings
compareTo(str) "abc".compareTo("abd") → -1
lexicographically
Bonus: Useful for Logic Building
Function Use case
charAt(i) + loop Check each character in string
toCharArray() + sort Useful in anagram problems
substring(i, j) Needed in palindrome or substring problems
split(" ") For word-level reversal, parsing
equals() and equalsIgnoreCase() For comparisons
replace() For pattern substitution
[Link]() Efficient string construction in loops
Array Functions
Top Java Array Functions & Techniques for DSA
1. Array Basics
Function/Concept Use / Example
Length of the array
[Link]
[Link] → 5
arr[i] Access element at index i
arr[i] = value Assign value to element at index i
for (int i=0; i<[Link]; i++) Basic iteration
2. Utility Functions (from Arrays class)
Import required:
java
CopyEdit
import [Link];
Function What it does
[Link](arr) Sorts array in ascending order
[Link](arr, [Link]()) Sorts in descending (only for Integer[])
[Link](arr, newLength) Copies array to new size
[Link](arr1, arr2) Compares two arrays
[Link](arr, value) Fills all elements with a value
[Link](arr) Converts array to printable string
[Link](arr, key) Binary search in sorted array
[Link](arr, from, to) Copy subarray from index from to to-1
3. Math Techniques Used in DSA
Concept Example / Use
Find sum sum += arr[i];
Find max if (arr[i] > max) max = arr[i];
Find min if (arr[i] < min) min = arr[i];
Prefix sum Running sum for range queries
Sliding window For fixed-size window operations
Two pointer Sorted array problems, pair sum, etc.
4. Looping Tricks
Use Case Code Example
Reverse array for (int i=n-1; i>=0; i--)
Swap two elements int temp=a; a=b; b=temp;
Iterate with 2 pointers int i=0, j=n-1; while(i<j)
5. Advanced Concepts (DSA)
Topic How it's used in arrays
Hashing Use int[] freq = new int[256]; to store frequencies
Sorting Use [Link]() or implement bubble/selection/merge sort
Searching Use binary search or linear search
Subarrays Nested loops / Kadane’s Algorithm
2D Arrays int[][] matrix = new int[3][3]; with nested loops
Most Common Array Use Cases in DSA
Use Case Functions / Code Used
Find largest/smallest element for loop with if
Reverse array Two-pointer swap logic
Remove duplicates Extra array / visited[]
Count frequency Frequency array
Rotate array Use temp array or reverse parts
Sort + Binary search [Link]() + [Link]()
Sum of subarray Nested loops or Kadane’s algorithm
Summary Cheat Table
Type Methods / Keywords
Basic [Link], arr[i]
Traversal for, for-each, while, 2 pointers
Sorting [Link](), custom sort
Copying [Link](), copyOfRange()
Searching [Link]()
Utility [Link](), equals(), fill()