diff --git a/Java/String_DSA_Java_Program/FirstOcuString_28.java b/Java/String_DSA_Java_Program/FirstOcuString_28.java new file mode 100644 index 00000000..24bc3f8e --- /dev/null +++ b/Java/String_DSA_Java_Program/FirstOcuString_28.java @@ -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)); + } +} + \ No newline at end of file diff --git a/Java/String_DSA_Java_Program/Longest_Common_P.java b/Java/String_DSA_Java_Program/Longest_Common_P.java new file mode 100644 index 00000000..106da96f --- /dev/null +++ b/Java/String_DSA_Java_Program/Longest_Common_P.java @@ -0,0 +1,3 @@ +public class Longest_Common_P { + +} diff --git a/Java/String_DSA_Java_Program/Remove_Duplicate_L_316.java b/Java/String_DSA_Java_Program/Remove_Duplicate_L_316.java new file mode 100644 index 00000000..0cde3e6c --- /dev/null +++ b/Java/String_DSA_Java_Program/Remove_Duplicate_L_316.java @@ -0,0 +1,3 @@ +public class Remove_Duplicate_L_316 { + +} diff --git a/Java/String_DSA_Java_Program/Valid_Palindron _125.java b/Java/String_DSA_Java_Program/Valid_Palindron _125.java new file mode 100644 index 00000000..e69de29b diff --git a/Java/String_DSA_Java_Program/Valid_Parentheses_20.java b/Java/String_DSA_Java_Program/Valid_Parentheses_20.java new file mode 100644 index 00000000..f170b539 --- /dev/null +++ b/Java/String_DSA_Java_Program/Valid_Parentheses_20.java @@ -0,0 +1,44 @@ +import java.util.*; +public class Valid_Parentheses_20 { + + public static boolean isValid(String str){ + + Stack s= new Stack<>(); + + for(int i=0; i