-
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
02484ce
commit ac5e2ed
Showing
4 changed files
with
83 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,54 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.util.*; | ||
import java.lang.*; | ||
import java.io.*; | ||
class GFG | ||
{ | ||
public static void main(String[] args) throws IOException | ||
{ | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int T = Integer.parseInt(br.readLine().trim()); | ||
while(T-->0) | ||
{ | ||
String s = br.readLine().trim(); | ||
Solution ob = new Solution(); | ||
List<String> ans = ob.AllPossibleStrings(s); | ||
for(String i: ans) | ||
System.out.print(i + " "); | ||
System.out.println(); | ||
|
||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
class Solution | ||
{ | ||
public List<String> AllPossibleStrings(String s) | ||
{ | ||
// Code here | ||
int n = s.length(); | ||
ArrayList<String>res=new ArrayList<>(); | ||
|
||
for (int m = 0; m < (1 << n); m++) | ||
{ | ||
String sb = ""; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if ((m & (1 << i))!=0) | ||
sb += s.charAt(i); | ||
} | ||
|
||
if (sb.length() > 0) | ||
res.add(sb); | ||
} | ||
|
||
Collections.sort(res); | ||
return res; | ||
} | ||
} |
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 * 2^n) | ||
Space complexity - O(n * 2^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,25 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* public class TreeNode { | ||
* int val; | ||
* TreeNode left; | ||
* TreeNode right; | ||
* TreeNode() {} | ||
* TreeNode(int val) { this.val = val; } | ||
* TreeNode(int val, TreeNode left, TreeNode right) { | ||
* this.val = val; | ||
* this.left = left; | ||
* this.right = right; | ||
* } | ||
* } | ||
*/ | ||
class Solution | ||
{ | ||
public boolean isSameTree(TreeNode p, TreeNode q) | ||
{ | ||
if (p == null || q == null) | ||
return p == q; | ||
|
||
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); | ||
} | ||
} |