0% found this document useful (0 votes)
14 views

Hashtable

The document discusses three approaches to solve the two sum problem: a brute force approach with nested loops, a two pass hash table approach, and an optimized one pass hash table approach. It also discusses three approaches to find the longest substring without repeating characters: a brute force approach, a sliding window approach, and an optimized sliding window approach.

Uploaded by

IG Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Hashtable

The document discusses three approaches to solve the two sum problem: a brute force approach with nested loops, a two pass hash table approach, and an optimized one pass hash table approach. It also discusses three approaches to find the longest substring without repeating characters: a brute force approach, a sliding window approach, and an optimized sliding window approach.

Uploaded by

IG Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that
they add up to target. You may assume that each input would have exactly one solution, and you
may not use the same element twice. You can return the answer in any order.

Approach 1|Brute Force:

class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == target - nums[i]) {
return new int[] { i, j };
}
}
}
return new int[]{};
}
}
Approach 2|Two-pass HashTable

class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i = 0; i < nums.length; i++)
{
hm.put(nums[i],i);
}
for(int i = 0; i < nums.length; i++){
if(hm.containsKey(target-nums[i]) && hm.get(target-nums[i])!=i)
{
return new int[]{hm.get(target-nums[i]),i};
}
}
return new int[]{};
}
}
Approach 3|One-pass HashTable|Best Approach:

class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target-nums[i])) {
return new int[] { map.get(target-nums[i]), i };
}
map.put(nums[i], i);
}
return new int[]{};
}
}
Longest substring without repeating characters

Given a string s, find the length of the longest substring without repeating characters.

Approach 1|Brute Force:

public class Solution {


public int lengthOfLongestSubstring(String s) {
int n = s.length();
int res = 0;
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
if (checkRepetition(s, i, j)) {
res = Math.max(res, j - i + 1);
}
}
}
return res;
}
private boolean checkRepetition(String s, int start, int end) {
Set<Character> chars = new HashSet<>();
for (int i = start; i <= end; i++) {
char c = s.charAt(i);
if(chars.contains(c)){
return false;
}
chars.add(c);
}
return true;
}
}
Approach 2|Sliding Window(TC=O(2n),SC(O(min(m,n)))

public class Solution {


public int lengthOfLongestSubstring(String s) {
Map<Character, Integer> chars = new HashMap();
int left = 0, right = 0, res = 0;
while (right < s.length()) {
char r = s.charAt(right);
if(!chars.containsKey(r)) chars.put(r,1);
else chars.put(r,chars.get(r)+1);
while (chars.get(r) > 1) {
char l = s.charAt(left);
chars.put(l, chars.get(l) - 1);
left++;
}
res = Math.max(res, right - left + 1);
right++;
}
return res;
}
}
Approach 3|Sliding Window optimized(TC=O(n),SC(O(min(m,n)))

public class Solution {


public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>();
for (int j = 0, i = 0; j < n; j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
}

You might also like