0% found this document useful (0 votes)
2 views5 pages

String and Array Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

String and Array Functions

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

StringBuilder.append() 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


arr.length
arr.length → 5

arr[i] Access element at index i

arr[i] = value Assign value to element at index i

for (int i=0; i<arr.length; i++) Basic iteration

2. Utility Functions (from Arrays class)


Import required:
java
CopyEdit
import java.util.Arrays;

Function What it does

Arrays.sort(arr) Sorts array in ascending order

Arrays.sort(arr, Collections.reverseOrder()) Sorts in descending (only for Integer[])

Arrays.copyOf(arr, newLength) Copies array to new size

Arrays.equals(arr1, arr2) Compares two arrays

Arrays.fill(arr, value) Fills all elements with a value

Arrays.toString(arr) Converts array to printable string

Arrays.binarySearch(arr, key) Binary search in sorted array

Arrays.copyOfRange(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 Arrays.sort() 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 Arrays.sort() + Arrays.binarySearch()

Sum of subarray Nested loops or Kadane’s algorithm

Summary Cheat Table

Type Methods / Keywords

Basic arr.length, arr[i]

Traversal for, for-each, while, 2 pointers

Sorting Arrays.sort(), custom sort

Copying Arrays.copyOf(), copyOfRange()

Searching Arrays.binarySearch()

Utility Arrays.toString(), equals(), fill()

You might also like