Skip to content

Commit 9497aff

Browse files
authored
Added tasks 2351-2357.
1 parent 08651c2 commit 9497aff

File tree

7 files changed

+440
-28
lines changed

7 files changed

+440
-28
lines changed

README.md

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
4747
|-|-|-|-|-|-
4848
| 0344 |[Reverse String](src/main/java/g0301_0400/s0344_reverse_string)| Easy | Top_Interview_Questions, String, Two_Pointers, Recursion | 1 | 99.91
49-
| 0557 |[Reverse Words in a String III](src/main/java/g0501_0600/s0557_reverse_words_in_a_string_iii)| Easy | String, Two_Pointers | 5 | 88.09
49+
| 0557 |[Reverse Words in a String III](src/main/java/g0501_0600/s0557_reverse_words_in_a_string_iii)| Easy | String, Two_Pointers | 4 | 97.75
5050

5151
#### Day 5 Two Pointers
5252

@@ -80,7 +80,7 @@
8080

8181
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
8282
|-|-|-|-|-|-
83-
| 0542 |[01 Matrix](src/main/java/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 6 | 96.78
83+
| 0542 |[01 Matrix](src/main/java/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 7 | 95.83
8484
| 0994 |[Rotting Oranges](src/main/java/g0901_1000/s0994_rotting_oranges)| Medium | Array, Breadth_First_Search, Matrix | 3 | 74.27
8585

8686
#### Day 10 Recursion Backtracking
@@ -165,7 +165,7 @@
165165
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
166166
|-|-|-|-|-|-
167167
| 0200 |[Number of Islands](src/main/java/g0101_0200/s0200_number_of_islands)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Depth_First_Search, Breadth_First_Search, Matrix, Union_Find | 3 | 97.76
168-
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 1 | 94.56
168+
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 2 | 69.51
169169

170170
#### Day 7 Breadth First Search Depth First Search
171171

@@ -928,7 +928,7 @@
928928
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
929929
|-|-|-|-|-|-
930930
| 1091 |[Shortest Path in Binary Matrix](src/main/java/g1001_1100/s1091_shortest_path_in_binary_matrix)| Medium | Array, Breadth_First_Search, Matrix | 22 | 69.99
931-
| 0542 |[01 Matrix](src/main/java/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 6 | 96.78
931+
| 0542 |[01 Matrix](src/main/java/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix | 7 | 95.83
932932

933933
#### Day 6 Matrix Related Problems
934934

@@ -948,7 +948,7 @@
948948

949949
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
950950
|-|-|-|-|-|-
951-
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 1 | 94.56
951+
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 2 | 69.51
952952
| 1319 |[Number of Operations to Make Network Connected](src/main/java/g1301_1400/s1319_number_of_operations_to_make_network_connected)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 9 | 67.64
953953

954954
#### Day 9 Standard Traversal
@@ -1238,7 +1238,7 @@
12381238

12391239
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
12401240
|-|-|-|-|-|-
1241-
| 0543 |[Diameter of Binary Tree](src/main/java/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 1 | 55.16
1241+
| 0543 |[Diameter of Binary Tree](src/main/java/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 1 | 65.86
12421242
| 0437 |[Path Sum III](src/main/java/g0401_0500/s0437_path_sum_iii)| Medium | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 18 | 45.66
12431243

12441244
#### Day 8 Binary Search
@@ -1326,7 +1326,7 @@
13261326

13271327
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
13281328
|-|-|-|-|-|-
1329-
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 1 | 94.56
1329+
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 2 | 69.51
13301330
| 0947 |[Most Stones Removed with Same Row or Column](src/main/java/g0901_1000/s0947_most_stones_removed_with_same_row_or_column)| Medium | Depth_First_Search, Graph, Union_Find | 5 | 98.95
13311331

13321332
#### Day 20 Brute Force/Backtracking
@@ -1470,7 +1470,7 @@
14701470
| 0103 |[Binary Tree Zigzag Level Order Traversal](src/main/java/g0101_0200/s0103_binary_tree_zigzag_level_order_traversal)| Medium | Top_Interview_Questions, Breadth_First_Search, Tree, Binary_Tree | 1 | 95.00
14711471
| 0108 |[Convert Sorted Array to Binary Search Tree](src/main/java/g0101_0200/s0108_convert_sorted_array_to_binary_search_tree)| Easy | Top_Interview_Questions, Array, Tree, Binary_Tree, Binary_Search_Tree, Divide_and_Conquer | 0 | 100.00
14721472
| 1008 |[Construct Binary Search Tree from Preorder Traversal](src/main/java/g1001_1100/s1008_construct_binary_search_tree_from_preorder_traversal)| Medium | Array, Tree, Binary_Tree, Stack, Monotonic_Stack, Binary_Search_Tree | 0 | 100.00
1473-
| 0543 |[Diameter of Binary Tree](src/main/java/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 1 | 55.16
1473+
| 0543 |[Diameter of Binary Tree](src/main/java/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree | 1 | 65.86
14741474
| 0938 |[Range Sum of BST](src/main/java/g0901_1000/s0938_range_sum_of_bst)| Easy | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 0 | 100.00
14751475
| 0100 |[Same Tree](src/main/java/g0001_0100/s0100_same_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
14761476
| 0226 |[Invert Binary Tree](src/main/java/g0201_0300/s0226_invert_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree | 0 | 100.00
@@ -1694,7 +1694,7 @@
16941694
|-|-|-|-|-|-
16951695
| 0334 |[Increasing Triplet Subsequence](src/main/java/g0301_0400/s0334_increasing_triplet_subsequence)| Medium | Top_Interview_Questions, Array, Greedy | 2 | 99.33
16961696
| 0238 |[Product of Array Except Self](src/main/java/g0201_0300/s0238_product_of_array_except_self)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Prefix_Sum | 1 | 100.00
1697-
| 0560 |[Subarray Sum Equals K](src/main/java/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum | 18 | 89.51
1697+
| 0560 |[Subarray Sum Equals K](src/main/java/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum | 21 | 98.97
16981698

16991699
#### Day 6 String
17001700

@@ -1816,6 +1816,11 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 2357 |[Make Array Zero by Subtracting Equal Amounts](src/main/java/g2301_2400/s2357_make_array_zero_by_subtracting_equal_amounts)| Easy | Array, Hash_Table, Sorting, Heap_Priority_Queue, Simulation | 1 | 98.24
1820+
| 2354 |[Number of Excellent Pairs](src/main/java/g2301_2400/s2354_number_of_excellent_pairs)| Hard | Array, Hash_Table, Binary_Search, Bit_Manipulation | 80 | 86.77
1821+
| 2353 |[Design a Food Rating System](src/main/java/g2301_2400/s2353_design_a_food_rating_system)| Medium | Hash_Table, Design, Heap_Priority_Queue, Ordered_Set | 364 | 83.35
1822+
| 2352 |[Equal Row and Column Pairs](src/main/java/g2301_2400/s2352_equal_row_and_column_pairs)| Medium | Array, Hash_Table, Matrix, Simulation | 7 | 98.94
1823+
| 2351 |[First Letter to Appear Twice](src/main/java/g2301_2400/s2351_first_letter_to_appear_twice)| Easy | Hash_Table, String, Counting | 1 | 63.38
18191824
| 2350 |[Shortest Impossible Sequence of Rolls](src/main/java/g2301_2400/s2350_shortest_impossible_sequence_of_rolls)| Hard | Array, Hash_Table, Greedy | 12 | 87.73
18201825
| 2349 |[Design a Number Container System](src/main/java/g2301_2400/s2349_design_a_number_container_system)| Medium | Hash_Table, Design, Heap_Priority_Queue, Ordered_Set | 208 | 54.57
18211826
| 2348 |[Number of Zero-Filled Subarrays](src/main/java/g2301_2400/s2348_number_of_zero_filled_subarrays)| Medium | Array, Math | 3 | 99.90
@@ -3213,27 +3218,27 @@
32133218
| 0566 |[Reshape the Matrix](src/main/java/g0501_0600/s0566_reshape_the_matrix)| Easy | Array, Matrix, Simulation, Data_Structure_I_Day_4_Array, Programming_Skills_I_Day_7_Array | 1 | 84.34
32143219
| 0565 |[Array Nesting](src/main/java/g0501_0600/s0565_array_nesting)| Medium | Array, Depth_First_Search | 4 | 98.54
32153220
| 0564 |[Find the Closest Palindrome](src/main/java/g0501_0600/s0564_find_the_closest_palindrome)| Hard | String, Math | 1 | 100.00
3216-
| 0563 |[Binary Tree Tilt](src/main/java/g0501_0600/s0563_binary_tree_tilt)| Easy | Depth_First_Search, Tree, Binary_Tree | 0 | 100.00
3217-
| 0561 |[Array Partition I](src/main/java/g0501_0600/s0561_array_partition_i)| Easy | Array, Sorting, Greedy, Counting_Sort | 18 | 31.77
3218-
| 0560 |[Subarray Sum Equals K](src/main/java/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 18 | 89.51
3219-
| 0559 |[Maximum Depth of N-ary Tree](src/main/java/g0501_0600/s0559_maximum_depth_of_n_ary_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree | 1 | 78.18
3221+
| 0563 |[Binary Tree Tilt](src/main/java/g0501_0600/s0563_binary_tree_tilt)| Easy | Depth_First_Search, Tree, Binary_Tree | 1 | 81.35
3222+
| 0561 |[Array Partition I](src/main/java/g0501_0600/s0561_array_partition_i)| Easy | Array, Sorting, Greedy, Counting_Sort | 14 | 84.99
3223+
| 0560 |[Subarray Sum Equals K](src/main/java/g0501_0600/s0560_subarray_sum_equals_k)| Medium | Top_100_Liked_Questions, Array, Hash_Table, Prefix_Sum, Data_Structure_II_Day_5_Array | 21 | 98.97
3224+
| 0559 |[Maximum Depth of N-ary Tree](src/main/java/g0501_0600/s0559_maximum_depth_of_n_ary_tree)| Easy | Depth_First_Search, Breadth_First_Search, Tree | 1 | 83.49
32203225
| 0558 |[Logical OR of Two Binary Grids Represented as Quad-Trees](src/main/java/g0501_0600/s0558_logical_or_of_two_binary_grids_represented_as_quad_trees)| Medium | Tree, Divide_and_Conquer | 0 | 100.00
3221-
| 0557 |[Reverse Words in a String III](src/main/java/g0501_0600/s0557_reverse_words_in_a_string_iii)| Easy | String, Two_Pointers, Algorithm_I_Day_4_Two_Pointers | 5 | 88.09
3226+
| 0557 |[Reverse Words in a String III](src/main/java/g0501_0600/s0557_reverse_words_in_a_string_iii)| Easy | String, Two_Pointers, Algorithm_I_Day_4_Two_Pointers | 4 | 97.75
32223227
| 0556 |[Next Greater Element III](src/main/java/g0501_0600/s0556_next_greater_element_iii)| Medium | String, Math, Two_Pointers, Programming_Skills_II_Day_10 | 0 | 100.00
3223-
| 0554 |[Brick Wall](src/main/java/g0501_0600/s0554_brick_wall)| Medium | Array, Hash_Table | 13 | 75.64
3224-
| 0553 |[Optimal Division](src/main/java/g0501_0600/s0553_optimal_division)| Medium | Array, Dynamic_Programming, Math | 1 | 96.63
3225-
| 0552 |[Student Attendance Record II](src/main/java/g0501_0600/s0552_student_attendance_record_ii)| Hard | Dynamic_Programming | 8 | 99.21
3226-
| 0551 |[Student Attendance Record I](src/main/java/g0501_0600/s0551_student_attendance_record_i)| Easy | String | 1 | 68.22
3227-
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_8_Standard_Traversal, Level_2_Day_19_Union_Find | 1 | 94.56
3228-
| 0546 |[Remove Boxes](src/main/java/g0501_0600/s0546_remove_boxes)| Hard | Array, Dynamic_Programming, Memoization | 86 | 60.93
3229-
| 0543 |[Diameter of Binary Tree](src/main/java/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 1 | 55.16
3230-
| 0542 |[01 Matrix](src/main/java/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_5_Matrix_Related_Problems | 6 | 96.78
3231-
| 0541 |[Reverse String II](src/main/java/g0501_0600/s0541_reverse_string_ii)| Easy | String, Two_Pointers | 1 | 93.18
3228+
| 0554 |[Brick Wall](src/main/java/g0501_0600/s0554_brick_wall)| Medium | Array, Hash_Table | 12 | 87.69
3229+
| 0553 |[Optimal Division](src/main/java/g0501_0600/s0553_optimal_division)| Medium | Array, Dynamic_Programming, Math | 0 | 100.00
3230+
| 0552 |[Student Attendance Record II](src/main/java/g0501_0600/s0552_student_attendance_record_ii)| Hard | Dynamic_Programming | 11 | 98.55
3231+
| 0551 |[Student Attendance Record I](src/main/java/g0501_0600/s0551_student_attendance_record_i)| Easy | String | 0 | 100.00
3232+
| 0547 |[Number of Provinces](src/main/java/g0501_0600/s0547_number_of_provinces)| Medium | Depth_First_Search, Breadth_First_Search, Graph, Union_Find, Algorithm_II_Day_6_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_8_Standard_Traversal, Level_2_Day_19_Union_Find | 2 | 69.51
3233+
| 0546 |[Remove Boxes](src/main/java/g0501_0600/s0546_remove_boxes)| Hard | Array, Dynamic_Programming, Memoization | 45 | 95.58
3234+
| 0543 |[Diameter of Binary Tree](src/main/java/g0501_0600/s0543_diameter_of_binary_tree)| Easy | Top_100_Liked_Questions, Depth_First_Search, Tree, Binary_Tree, Level_2_Day_7_Tree, Udemy_Tree_Stack_Queue | 1 | 65.86
3235+
| 0542 |[01 Matrix](src/main/java/g0501_0600/s0542_01_matrix)| Medium | Array, Dynamic_Programming, Breadth_First_Search, Matrix, Algorithm_I_Day_9_Breadth_First_Search_Depth_First_Search, Graph_Theory_I_Day_5_Matrix_Related_Problems | 7 | 95.83
3236+
| 0541 |[Reverse String II](src/main/java/g0501_0600/s0541_reverse_string_ii)| Easy | String, Two_Pointers | 1 | 100.00
32323237
| 0540 |[Single Element in a Sorted Array](src/main/java/g0501_0600/s0540_single_element_in_a_sorted_array)| Medium | Array, Binary_Search, Binary_Search_II_Day_9 | 0 | 100.00
3233-
| 0539 |[Minimum Time Difference](src/main/java/g0501_0600/s0539_minimum_time_difference)| Medium | Array, String, Math, Sorting | 3 | 93.52
3234-
| 0538 |[Convert BST to Greater Tree](src/main/java/g0501_0600/s0538_convert_bst_to_greater_tree)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 1 | 83.78
3235-
| 0537 |[Complex Number Multiplication](src/main/java/g0501_0600/s0537_complex_number_multiplication)| Medium | String, Math, Simulation | 7 | 62.22
3236-
| 0535 |[Encode and Decode TinyURL](src/main/java/g0501_0600/s0535_encode_and_decode_tinyurl)| Medium | String, Hash_Table, Design, Hash_Function | 4 | 65.42
3238+
| 0539 |[Minimum Time Difference](src/main/java/g0501_0600/s0539_minimum_time_difference)| Medium | Array, String, Math, Sorting | 2 | 99.62
3239+
| 0538 |[Convert BST to Greater Tree](src/main/java/g0501_0600/s0538_convert_bst_to_greater_tree)| Medium | Depth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 1 | 88.41
3240+
| 0537 |[Complex Number Multiplication](src/main/java/g0501_0600/s0537_complex_number_multiplication)| Medium | String, Math, Simulation | 6 | 71.88
3241+
| 0535 |[Encode and Decode TinyURL](src/main/java/g0501_0600/s0535_encode_and_decode_tinyurl)| Medium | String, Hash_Table, Design, Hash_Function | 2 | 89.67
32373242
| 0532 |[K-diff Pairs in an Array](src/main/java/g0501_0600/s0532_k_diff_pairs_in_an_array)| Medium | Array, Hash_Table, Sorting, Binary_Search, Two_Pointers, Udemy_Arrays | 13 | 58.23
32383243
| 0530 |[Minimum Absolute Difference in BST](src/main/java/g0501_0600/s0530_minimum_absolute_difference_in_bst)| Easy | Depth_First_Search, Breadth_First_Search, Tree, Binary_Tree, Binary_Search_Tree | 1 | 92.05
32393244
| 0529 |[Minesweeper](src/main/java/g0501_0600/s0529_minesweeper)| Medium | Array, Depth_First_Search, Breadth_First_Search, Matrix | 0 | 100.00

src/main/java/g0501_0600/s0560_subarray_sum_equals_k/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public class Solution {
4848
sumCount.put(tempSum, 1);
4949
}
5050
}
51-
5251
return ret;
5352
}
5453
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 2351\. First Letter to Appear Twice
5+
6+
Easy
7+
8+
Given a string `s` consisting of lowercase English letters, return _the first letter to appear **twice**_.
9+
10+
**Note**:
11+
12+
* A letter `a` appears twice before another letter `b` if the **second** occurrence of `a` is before the **second** occurrence of `b`.
13+
* `s` will contain at least one letter that appears twice.
14+
15+
**Example 1:**
16+
17+
**Input:** s = "abccbaacz"
18+
19+
**Output:** "c"
20+
21+
**Explanation:**
22+
23+
The letter 'a' appears on the indexes 0, 5 and 6.
24+
25+
The letter 'b' appears on the indexes 1 and 4.
26+
27+
The letter 'c' appears on the indexes 2, 3 and 7.
28+
29+
The letter 'z' appears on the index 8.
30+
31+
The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
32+
33+
**Example 2:**
34+
35+
**Input:** s = "abcdd"
36+
37+
**Output:** "d"
38+
39+
**Explanation:**
40+
41+
The only letter that appears twice is 'd' so we return 'd'.
42+
43+
**Constraints:**
44+
45+
* `2 <= s.length <= 100`
46+
* `s` consists of lowercase English letters.
47+
* `s` has at least one repeated letter.
48+
49+
## Solution
50+
51+
```java
52+
import java.util.HashMap;
53+
54+
public class Solution {
55+
public char repeatedCharacter(String s) {
56+
HashMap<Character, Integer> map = new HashMap<>();
57+
for (int i = 0; i < s.length(); i++) {
58+
if (map.containsKey(s.charAt(i))) {
59+
if (map.get(s.charAt(i)) >= 1) {
60+
return s.charAt(i);
61+
}
62+
} else {
63+
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
64+
}
65+
}
66+
return 'c';
67+
}
68+
}
69+
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Java?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Java)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Java?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Java/fork)
3+
4+
## 2352\. Equal Row and Column Pairs
5+
6+
Medium
7+
8+
Given a **0-indexed** `n x n` integer matrix `grid`, _return the number of pairs_ <code>(R<sub>i</sub>, C<sub>j</sub>)</code> _such that row_ <code>R<sub>i</sub></code> _and column_ <code>C<sub>j</sub></code> _are equal_.
9+
10+
A row and column pair is considered equal if they contain the same elements in the same order (i.e. an equal array).
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2022/06/01/ex1.jpg)
15+
16+
**Input:** grid = \[\[3,2,1],[1,7,6],[2,7,7]]
17+
18+
**Output:** 1
19+
20+
**Explanation:** There is 1 equal row and column pair:
21+
22+
- (Row 2, Column 1): [2,7,7]
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2022/06/01/ex2.jpg)
27+
28+
**Input:** grid = \[\[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]
29+
30+
**Output:** 3
31+
32+
**Explanation:** There are 3 equal row and column pairs:
33+
34+
- (Row 0, Column 0): [3,1,2,2]
35+
36+
- (Row 2, Column 2): [2,4,2,2]
37+
38+
- (Row 3, Column 2): [2,4,2,2]
39+
40+
**Constraints:**
41+
42+
* `n == grid.length == grid[i].length`
43+
* `1 <= n <= 200`
44+
* <code>1 <= grid[i][j] <= 10<sup>5</sup></code>
45+
46+
## Solution
47+
48+
```java
49+
import java.util.Arrays;
50+
import java.util.HashMap;
51+
import java.util.Map;
52+
53+
public class Solution {
54+
public int equalPairs(int[][] grid) {
55+
int[] tmpCol = new int[grid.length];
56+
Map<Integer, Integer> pairsMap = new HashMap<>();
57+
int pairsCounter = 0;
58+
for (int col = 0; col < grid[0].length; col++) {
59+
for (int row = 0; row < grid.length; row++) {
60+
tmpCol[row] = grid[row][col];
61+
}
62+
int hashCode = Arrays.hashCode(tmpCol);
63+
pairsMap.put(hashCode, pairsMap.getOrDefault(hashCode, 0) + 1);
64+
}
65+
for (int[] row : grid) {
66+
int hashCode = Arrays.hashCode(row);
67+
if (pairsMap.containsKey(hashCode)) {
68+
pairsCounter += pairsMap.get(hashCode);
69+
}
70+
}
71+
return pairsCounter;
72+
}
73+
}
74+
```

0 commit comments

Comments
 (0)