Visitar URL original
Adding LeetCode Solutions by Deepanshuchauhan123 · Pull Request #2201 · TheAlgorithms/Java · GitHub
The Wayback Machine - https://web.archive.org/web/20210605234501/https://github.com/TheAlgorithms/Java/pull/2201/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding LeetCode Solutions #2201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter
Filter file types
Beta Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.

Always

Just for now

@@ -0,0 +1,39 @@
import java.util.*;

class Problem139_wordBreak{

public static void main(String arr[]) {
Scanner sc = new Scanner(System.in);
String value = sc.nextLine();
ArrayList list = new ArrayList<String>();
list.add("apple");
list.add("pen");
// list.add("Le");

if (wordBreak(value, list))
System.out.println("Valid Word");
else
System.out.println("Invalid");

}

public static boolean wordBreak(String s, List<String> wordDict) {

boolean dp[] = new boolean[s.length() + 1];
dp[0] = true;

Set<String> set = new HashSet<>();
for (String word : wordDict)
set.add(word);

for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
if (dp[j] && set.contains(s.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}
}
@@ -0,0 +1,57 @@

import java.util.Scanner;
import java.util.Stack;

class Problem_394_DecodeString{

public static void main(String arr[])
{
Scanner sc = new Scanner(System.in);
String value = sc.nextLine();

System.out.println(decode(value));
}

public static String decode(String s) {

Stack<Integer> numStack = new Stack<>();
Stack<String> strStrack = new Stack<>();

StringBuilder sb = new StringBuilder();

int n = s.length();

for(int i=0;i<n;i++)
{
char ch = s.charAt(i);
if(Character.isDigit(ch))
{
int num = ch-'0';
while (i<n && Character.isDigit(s.charAt(i+1)))
{
num = num*10 + s.charAt(i+1)-'0';
i++;
}
numStack.push(num);
}
else if(s.charAt(i)=='[')
{
strStrack.push(sb.toString());
sb = new StringBuilder();
}
else if(s.charAt(i)==']')
{
StringBuilder temp = new StringBuilder(strStrack.pop());
int rep = numStack.pop();
for(int j =0;j<rep;j++)
temp.append(sb);
sb= temp;

}else{
sb.append(ch);
}
}
return sb.toString();

}
}
ProTip! Use n and p to navigate between commits in a pull request.