Skip to content

Commit 154f106

Browse files
author
Vinay Patel
committed
Array_And_Hashing Questions Finish
1 parent f980535 commit 154f106

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Arrays_And_Hashing;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class Encode_and_Decode_Strings_8 {
7+
public static void main(String[] args) {
8+
Encode_and_Decode_Strings_8 solution = new Encode_and_Decode_Strings_8();
9+
10+
// Example usage:
11+
List<String> inputList = new ArrayList<>();
12+
inputList.add("Hello");
13+
inputList.add("World");
14+
inputList.add("Java");
15+
16+
// Encoding
17+
String encodedString = solution.encode(inputList);
18+
System.out.println("Encoded String: " + encodedString);
19+
20+
// Decoding
21+
List<String> decodedList = solution.decode(encodedString);
22+
System.out.println("Decoded List: " + decodedList);
23+
}
24+
25+
public String encode(List<String> strs) {
26+
StringBuilder encodedString = new StringBuilder();
27+
for (String str : strs) {
28+
encodedString.append(str.length()).append("#").append(str);
29+
}
30+
return encodedString.toString();
31+
}
32+
33+
public List<String> decode(String str) {
34+
List<String> list = new ArrayList<>();
35+
int i = 0;
36+
while (i < str.length()) {
37+
int j = i;
38+
while (str.charAt(j) != '#')
39+
j++;
40+
41+
int length = Integer.valueOf(str.substring(i, j));
42+
i = j + 1 + length;
43+
list.add(str.substring(j + 1, i));
44+
}
45+
return list;
46+
}
47+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package Arrays_And_Hashing;
2+
3+
import java.util.HashSet;
4+
5+
public class Longest_Consecutive_Sequence_9 {
6+
public static void main(String[] args) {
7+
Longest_Consecutive_Sequence_9 solution = new Longest_Consecutive_Sequence_9();
8+
9+
// Example 1:
10+
int[] nums1 = { 100, 4, 200, 1, 3, 2 };
11+
int result1 = solution.longestConsecutive(nums1);
12+
System.out.println("Example 1 Output: " + result1);
13+
14+
// Example 2:
15+
int[] nums2 = { 0, 3, 7, 2, 5, 8, 4, 6, 0, 1 };
16+
int result2 = solution.longestConsecutive(nums2);
17+
System.out.println("Example 2 Output: " + result2);
18+
}
19+
20+
public int longestConsecutive(int[] nums) {
21+
22+
if (nums == null || nums.length == 0)
23+
return 0;
24+
25+
HashSet<Integer> numSet = new HashSet<>();
26+
for (int num : nums) {
27+
numSet.add(num);
28+
}
29+
30+
int maxLength = 0;
31+
32+
for (int num : numSet) {
33+
// If the current number is the start of a sequence
34+
if (!numSet.contains(num - 1)) {
35+
int currentLength = 1;
36+
while (numSet.contains(num + 1)) {
37+
num++;
38+
currentLength++;
39+
}
40+
maxLength = Math.max(maxLength, currentLength);
41+
}
42+
}
43+
44+
return maxLength;
45+
46+
// Below Is Using Hash Map
47+
48+
/*
49+
* if (nums.length == 0)
50+
* return 0;
51+
* HashSet<Integer> hs = new HashSet<>();
52+
* for (int num : nums)
53+
* hs.add(num);
54+
* int longest = 1;
55+
* for (int num : nums) {
56+
* // check if the num is the start of a sequence by checking if left exists
57+
* if (!hs.contains(num - 1)) { // start of a sequence
58+
* int count = 1;
59+
* while (hs.contains(num + 1)) { // check if hs contains next no.
60+
* num++;
61+
* count++;
62+
* }
63+
* longest = Math.max(longest, count);
64+
*
65+
* }
66+
* if (longest > nums.length / 2)
67+
* break;
68+
*
69+
* }
70+
* return longest;
71+
*
72+
*/
73+
}
74+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package Arrays_And_Hashing;
2+
3+
public class Product_of_Array_Except_Self_6 {
4+
public static void main(String[] args) {
5+
6+
}
7+
8+
public int[] productExceptSelf(int[] nums) {
9+
10+
// Brute Force Give TLE :)
11+
12+
// int[] result = new int[nums.length];
13+
// for (int i = 0; i < nums.length; i++) {
14+
// // int temp = nums[i];
15+
// int temp = 1;
16+
// for (int j = 0; j < nums.length; j++) {
17+
// // int ans = temp*nums[j];
18+
// // return new int[]{ans};
19+
// if (i != j) {
20+
// temp *= nums[j];
21+
// }
22+
// }
23+
// result[i] = temp;
24+
// }
25+
// // return null;
26+
// return result;
27+
28+
int n = nums.length;
29+
int[] result = new int[n];
30+
31+
// int prefixProduct = 1;
32+
int Prefixtemp = 1;
33+
for (int i = 0; i < n; i++) {
34+
result[i] = Prefixtemp;
35+
Prefixtemp *= nums[i];
36+
}
37+
int Suffixtemp = 1;
38+
for (int i = n - 1; i >= 0; i--) {
39+
result[i] *= Suffixtemp;
40+
Suffixtemp *= nums[i];
41+
}
42+
return result;
43+
}
44+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package Arrays_And_Hashing;
2+
3+
import java.util.HashMap;
4+
import java.util.HashSet;
5+
import java.util.Map;
6+
import java.util.Set;
7+
8+
public class Valid_Sudoku_7 {
9+
public static void main(String[] args) {
10+
11+
}
12+
13+
public boolean isValidSudoku(char[][] board) {
14+
Map<Integer, Set<Character>> cols = new HashMap<>();
15+
Map<Integer, Set<Character>> rows = new HashMap<>();
16+
Map<Integer, Set<Character>> squares = new HashMap<>();
17+
18+
for (int r = 0; r < 9; r++) {
19+
for (int c = 0; c < 9; c++) {
20+
if (board[r][c] == '.') {
21+
continue;
22+
}
23+
char num = board[r][c];
24+
if (rows.containsKey(r) && rows.get(r).contains(num) ||
25+
cols.containsKey(c) && cols.get(c).contains(num) ||
26+
squares.containsKey((r / 3) * 3 + c / 3) && squares.get((r / 3) * 3 + c / 3).contains(num)) {
27+
return false;
28+
}
29+
30+
rows.computeIfAbsent(r, k -> new HashSet<>()).add(num);
31+
cols.computeIfAbsent(c, k -> new HashSet<>()).add(num);
32+
squares.computeIfAbsent((r / 3) * 3 + c / 3, k -> new HashSet<>()).add(num);
33+
}
34+
}
35+
36+
return true;
37+
}
38+
}

0 commit comments

Comments
 (0)