-
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
425fd04
commit 2e0642c
Showing
4 changed files
with
72 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,56 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
public class GfG | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while(t-->0) | ||
{ | ||
int n; | ||
n = sc.nextInt(); | ||
ArrayList<String> arr = new ArrayList<String>(); | ||
for(int i = 0;i<n;i++) | ||
{ | ||
String p = sc.next(); | ||
arr.add(p); | ||
} | ||
String line = sc.next(); | ||
Solution obj = new Solution(); | ||
System.out.println(obj.wordBreak(line,arr)); | ||
|
||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
public static int wordBreak(String A, ArrayList<String> B ) | ||
{ | ||
//code here | ||
int n = A.length(); | ||
HashSet<String> wordSet = new HashSet<>(B); | ||
boolean[] dp = new boolean[n + 1]; | ||
dp[0] = true; | ||
for (int i = 1; i <= n; i++) | ||
{ | ||
for (int j = 0; j < i; j++) | ||
{ | ||
if (dp[j] && wordSet.contains(A.substring(j, i))) | ||
{ | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
return dp[n] ? 1 : 0; | ||
} | ||
} |
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) | ||
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(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,12 @@ | ||
class Solution | ||
{ | ||
public int missingNumber(int[] nums) | ||
{ | ||
int ans = nums.length; | ||
|
||
for (int i = 0; i < nums.length; ++i) | ||
ans ^= i ^ nums[i]; | ||
|
||
return ans; | ||
} | ||
} |