-
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
a7bac35
commit d8f4143
Showing
4 changed files
with
74 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,48 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
|
||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.*; | ||
|
||
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()); | ||
while (tc-- > 0) { | ||
String str = br.readLine().trim(); | ||
|
||
String ans = new Solution().removeDuplicates(str); | ||
System.out.println(ans); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
|
||
class Solution | ||
{ | ||
String removeDuplicates(String str) | ||
{ | ||
Map<Character,Integer> exists = new HashMap<>(); | ||
|
||
String ans = ""; | ||
for(int i = 0; i < str.length(); i++) | ||
{ | ||
if(!exists.containsKey(str.charAt(i))) | ||
{ | ||
ans += str.charAt(i); | ||
exists.put(str.charAt(i), 1); | ||
} | ||
} | ||
|
||
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(n) | ||
Space complexity - O(n) |
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 complexity - O(n) |
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,22 @@ | ||
class Solution | ||
{ | ||
public int[] intersection(int[] nums1, int[] nums2) | ||
{ | ||
Set<Integer> set = new HashSet<>(); | ||
Set<Integer> intersect = new HashSet<>(); | ||
for (int i = 0; i < nums1.length; i++) | ||
set.add(nums1[i]); | ||
|
||
for (int i = 0; i < nums2.length; i++) | ||
if (set.contains(nums2[i])) | ||
intersect.add(nums2[i]); | ||
|
||
int[] result = new int[intersect.size()]; | ||
int i = 0; | ||
|
||
for (Integer num : intersect) | ||
result[i++] = num; | ||
|
||
return result; | ||
} | ||
} |