[go: up one dir, main page]

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

Array and String Tecniques

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)
11 views5 pages

Array and String Tecniques

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

1.

Frequency Count (Counting Array)

Use When:

• You need to count how often elements/characters appear.

Common Problems:

• Check if two strings are anagrams


• Count duplicate characters or numbers
• First non-repeating character
• Can a string be rearranged into a palindrome

Java Code (No inbuilt):

java
CopyEdit
int[] freq = new int[26]; // for lowercase characters
for (int i = 0; i < str.length(); i++) {
freq[str.charAt(i) - 'a']++;
}

2. Two Pointers Technique

Use When:

• You want to check from both ends or move two pointers at different speeds.

Common Problems:

• Reverse string/array
• Check palindrome
• Remove duplicates from sorted array
• Move zeros to the end

Reverse Example:

java
CopyEdit
int start = 0, end = arr.length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}

3. Sliding Window

Use When:

• You need to work with subarrays or substrings of fixed or variable size.

Common Problems:

• Maximum sum subarray of size K


• Longest substring without repeating characters
• Count anagrams in a string

Example (Sum of window size K):

java
CopyEdit
int maxSum = 0, windowSum = 0, k = 3;
for (int i = 0; i < k; i++) windowSum += arr[i];
maxSum = windowSum;
for (int i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
if (windowSum > maxSum) maxSum = windowSum;
}

4. Hashing (Custom Arrays Instead of HashMap)

Use When:

• You need fast lookup/check if element was seen.

Common Problems:

• Detect duplicates
• Count frequency
• Group anagrams
• Two sum (find pair with given sum)
(Use boolean[] or int[] instead of HashMap if values are in known range)
5. Prefix Sum (Cumulative Sum)

Use When:

• You need to answer range sum queries or subarray sums fast.

Common Problems:

• Subarray sum equals target


• Count subarrays with even/odd sum
• Difference between two ranges

Example:

java
CopyEdit
int[] prefix = new int[arr.length];
prefix[0] = arr[0];
for (int i = 1; i < arr.length; i++) {
prefix[i] = prefix[i - 1] + arr[i];
}
// Sum of range [l, r] = prefix[r] - prefix[l-1]

6. Sorting + Binary Search (Manual Sort & Search)

Use When:

• You want to find elements quickly after sorting.

Common Problems:

• Kth smallest/largest
• Search in sorted rotated array
• Pair with given sum (using binary search)

Bubble Sort (Without inbuilt):

java
CopyEdit
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}

7. Stack & Queue Based (Manual Arrays)

Use When:

• Order of processing matters (LIFO or FIFO)

Common Problems:

• Balanced parentheses
• Next greater element
• Remove duplicates (like backspace string)

Manual Stack Using Array:

java
CopyEdit
char[] stack = new char[str.length()];
int top = -1;

for (int i = 0; i < str.length(); i++) {


char ch = str.charAt(i);
if (top >= 0 && stack[top] == ch) top--;
else stack[++top] = ch;
}

8. Brute Force

Use When:

• Problem is small, or you are finding patterns.

Common Problems:

• Generate all substrings


• Try all pairs or triplets
• Test all rotations

Example: Find duplicate using nested loop

java
CopyEdit
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
System.out.println("Duplicate: " + arr[i]);
}
}
}

Summary Table for Array & String:

Technique Use Case Examples

Frequency Count Anagram, Palindrome, Frequency count

Two Pointers Reverse, Palindrome, Remove duplicates

Sliding Window Substrings, Maximum/Minimum sum

Hashing (int[]) Duplicates, Two-sum, Count frequency

Prefix Sum Range sums, Subarrays

Sorting + Search Kth element, Rotated array search

Stack/Queue Balanced parens, Next greater

Brute Force Pairs, Triplets, All substrings

You might also like