Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Java/String_DSA_Java_Program/FirstOcuString_28.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.



// Example 1:

// Input: haystack = "sadbutsad", needle = "sad"
// Output: 0
// Explanation: "sad" occurs at index 0 and 6.
// The first occurrence is at index 0, so we return 0.
// Example 2:

// Input: haystack = "leetcode", needle = "leeto"
// Output: -1
// Explanation: "leeto" did not occur in "leetcode", so we return -1.

public class FirstOcuString_28 {


public static int substring(String s1, String s2){

if (s2.isEmpty()) {
return 0;
}

int n=s1.length(); // haystack
int m=s2.length(); // needle
for (int i = 0; i <= n- m; i++) {

int j=0;

while (j < m && s1.charAt(i + j) == s2.charAt(j)) {
j++;
}

if (j == m) {
return i;
}

}
return -1;
}

// Another Approach
public static int strtr(String haystack, String needle){

for(int i=0; i< haystack.length(); i++){
if (haystack.charAt(i) == needle.charAt(0)) {
int j=0;// needle
int k=i; // haystack

while (j < needle.length() && k < haystack.length() && haystack.charAt(k) == needle.charAt(j)) {
j++;
k++;
}
if (j == needle.length()) {
return i;
}
}
}
return -1;
}

public static void main(String[] args) {
String haystack = "sadbutsad";

String tar_needle = "sad";

System.out.println(strtr(haystack, tar_needle));
System.out.println(substring(haystack, tar_needle));
}
}

3 changes: 3 additions & 0 deletions Java/String_DSA_Java_Program/Longest_Common_P.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Longest_Common_P {

}
3 changes: 3 additions & 0 deletions Java/String_DSA_Java_Program/Remove_Duplicate_L_316.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Remove_Duplicate_L_316 {

}
Empty file.
44 changes: 44 additions & 0 deletions Java/String_DSA_Java_Program/Valid_Parentheses_20.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.*;
public class Valid_Parentheses_20 {

public static boolean isValid(String str){

Stack<Character> s= new Stack<>();

for(int i=0; i<str.length(); i++){

char ch= str.charAt(i);

if (ch == '{' || ch =='(' || ch== '[') { // opening
s.push(ch);

}else{

if(s.isEmpty()) {
return false;
}
if (s.peek()== '{' && ch== '}' ||
(s.peek() == '(') && ch == ')'||
(s.peek() == '[') && ch == ']') {
s.pop();
}else{
return false;
}
}
}
if (s.isEmpty()) {
return true;
}else{
return false;
}
}




public static void main(String[] args) {
String str= "[()]{}";
System.out.println(isValid(str));

}
}