-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a57d32c
commit bd4ad26
Showing
4 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//{ Driver Code Starts | ||
import java.util.*; | ||
import java.io.*; | ||
|
||
public class Main { | ||
|
||
public static void main(String[] args) throws Exception { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int tc = Integer.parseInt(br.readLine().trim()); | ||
while (tc-- > 0) { | ||
String[] inputLine; | ||
inputLine = br.readLine().trim().split(" "); | ||
int n = Integer.parseInt(inputLine[0]); | ||
int m = Integer.parseInt(inputLine[1]); | ||
int[][] arr = new int[n][m]; | ||
|
||
for(int i = 0; i < n; i++){ | ||
String[] s = br.readLine().trim().split(" "); | ||
for(int j = 0; j < m; j++){ | ||
arr[i][j] = Integer.parseInt(s[j]); | ||
} | ||
} | ||
ArrayList<Integer> ans = new Solution().repeatedRows(arr, n, m); | ||
for(int i=0;i<ans.size();i++) | ||
System.out.print(ans.get(i)+" "); | ||
|
||
System.out.println(); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
public static ArrayList<Integer> repeatedRows(int matrix[][], int m, int n) | ||
{ | ||
//code here | ||
TreeSet<Integer> set = new TreeSet<>(); | ||
|
||
ArrayList<Integer> ans = new ArrayList<>(); | ||
|
||
for(int i = 0; i < m; i++) | ||
{ | ||
int num = 0; | ||
for(int j = 0; j < n; j++) | ||
{ | ||
num += matrix[i][j] << j; | ||
} | ||
|
||
if(set.contains(num)) | ||
ans.add(i); | ||
|
||
else | ||
set.add(num); | ||
} | ||
|
||
return ans; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(m*n) | ||
Space complexity - O(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complextiy - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Solution | ||
{ | ||
public boolean closeStrings(String word1, String word2) | ||
{ | ||
if (word1.length() != word2.length()) | ||
return false; | ||
|
||
Map<Character, Integer> count1 = new HashMap<>(); | ||
Map<Character, Integer> count2 = new HashMap<>(); | ||
|
||
for (final char c : word1.toCharArray()) | ||
count1.merge(c, 1, Integer::sum); | ||
|
||
for (final char c : word2.toCharArray()) | ||
count2.merge(c, 1, Integer::sum); | ||
|
||
if (!count1.keySet().equals(count2.keySet())) | ||
return false; | ||
|
||
List<Integer> freqs1 = new ArrayList<>(count1.values()); | ||
List<Integer> freqs2 = new ArrayList<>(count2.values()); | ||
|
||
Collections.sort(freqs1); | ||
Collections.sort(freqs2); | ||
|
||
return freqs1.equals(freqs2); | ||
} | ||
} |