Skip to content

Commit db1fcc2

Browse files
Merge pull request #203 from anishkumar127/v8
code
2 parents 0caabab + b2f763a commit db1fcc2

File tree

3 files changed

+284
-0
lines changed

3 files changed

+284
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
```java
2+
//{ Driver Code Starts
3+
//Initial Template for Java
4+
5+
/*package whatever //do not write package name here */
6+
7+
import java.io.*;
8+
import java.util.*;
9+
import java.math.*;
10+
11+
class Node
12+
{
13+
int data;
14+
Node left, right;
15+
16+
public Node(int d)
17+
{
18+
data = d;
19+
left = right = null;
20+
}
21+
}
22+
23+
class GFG
24+
{
25+
static Node buildTree(String str)
26+
{
27+
// Corner Case
28+
if(str.length() == 0 || str.equals('N'))
29+
return null;
30+
String[] s = str.split(" ");
31+
32+
Node root = new Node(Integer.parseInt(s[0]));
33+
Queue <Node> q = new LinkedList<Node>();
34+
q.add(root);
35+
36+
// Starting from the second element
37+
int i = 1;
38+
while(!q.isEmpty() && i < s.length)
39+
{
40+
// Get and remove the front of the queue
41+
Node currNode = q.remove();
42+
43+
// Get the current node's value from the string
44+
String currVal = s[i];
45+
46+
// If the left child is not null
47+
if(!currVal.equals("N"))
48+
{
49+
50+
// Create the left child for the current node
51+
currNode.left = new Node(Integer.parseInt(currVal));
52+
53+
// Push it to the queue
54+
q.add(currNode.left);
55+
}
56+
57+
// For the right child
58+
i++;
59+
if(i >= s.length)
60+
break;
61+
currVal = s[i];
62+
63+
// If the right child is not null
64+
if(!currVal.equals("N"))
65+
{
66+
67+
// Create the right child for the current node
68+
currNode.right = new Node(Integer.parseInt(currVal));
69+
70+
// Push it to the queue
71+
q.add(currNode.right);
72+
}
73+
74+
i++;
75+
}
76+
77+
return root;
78+
}
79+
80+
public static void main(String args[]) throws IOException {
81+
82+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
83+
int t = Integer.parseInt(br.readLine().trim());
84+
while(t>0)
85+
{
86+
String s = br.readLine();
87+
Node root = buildTree(s);
88+
89+
int target = Integer.parseInt(br.readLine().trim());
90+
91+
Solution T = new Solution();
92+
System.out.println(T.isPairPresent(root,target));
93+
t--;
94+
}
95+
}
96+
}
97+
98+
// } Driver Code Ends
99+
100+
101+
//User function Template for Java
102+
103+
class Solution
104+
{
105+
// root : the root Node of the given BST
106+
// target : the target sum
107+
private void inOrder(Node root, ArrayList<Integer>ans){
108+
if(root==null) return;
109+
110+
inOrder(root.left,ans);
111+
ans.add(root.data);
112+
inOrder(root.right,ans);
113+
}
114+
public int isPairPresent(Node root, int target)
115+
{
116+
117+
ArrayList<Integer> ans = new ArrayList<>();
118+
119+
inOrder(root,ans);
120+
// Write your code here
121+
int s = 0;
122+
int e = ans.size()-1;
123+
while(s<=e){
124+
int sum = ans.get(s) + ans.get(e);
125+
if(sum==target) return 1;
126+
else if(sum<target) s++;
127+
else e--;
128+
}
129+
return 0;
130+
}
131+
}
132+
133+
134+
```
135+
136+
137+
https://practice.geeksforgeeks.org/problems/find-a-pair-with-given-target-in-bst/1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/
2+
3+
4+
# [28\. Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/)
5+
6+
## Description
7+
8+
Difficulty: **Medium**
9+
10+
Related Topics: [Two Pointers](https://leetcode.com/tag/two-pointers/), [String](https://leetcode.com/tag/string/), [String Matching](https://leetcode.com/tag/string-matching/)
11+
12+
13+
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`.
14+
15+
**Example 1:**
16+
17+
```
18+
Input: haystack = "sadbutsad", needle = "sad"
19+
Output: 0
20+
Explanation: "sad" occurs at index 0 and 6.
21+
The first occurrence is at index 0, so we return 0.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: haystack = "leetcode", needle = "leeto"
28+
Output: -1
29+
Explanation: "leeto" did not occur in "leetcode", so we return -1.
30+
```
31+
32+
**Constraints:**
33+
34+
* 1 <= haystack.length, needle.length <= 10<sup>4</sup>
35+
* `haystack` and `needle` consist of only lowercase English characters.
36+
37+
38+
## Solution
39+
40+
Language: **Java**
41+
42+
```java
43+
class Solution {
44+
   public int strStr(String haystack, String needle) {
45+
       for(int i=0; i<haystack.length() - needle.length()+1; i++){  // 9-3+1 = 7;
46+
           
47+
           if(haystack.charAt(i)==needle.charAt(0)){
48+
               if(haystack.substring(i,needle.length()+i).equals(needle)){
49+
                   return i;
50+
              }
51+
          }
52+
           
53+
      }
54+
       return -1;
55+
  }
56+
}
57+
58+
/*
59+
60+
haystack.length() - needle.length() + 1; // why ? because sad start from s. and if s in the end of the length of haystack. then how we can get the substring ? its give the error out of bound.
61+
62+
so we run till the haystack length and - needle length + 1.
63+
64+
65+
and now inside for loop we will check if haystack character match the first character of the needle.
66+
then we check the complete sentence of the needle using substring method. and we apply the substring method inside the haystack.
67+
and if it is equal then return the index.
68+
else not equal we check till the length. in the end we return the -1 as given the question requirements.
69+
70+
*/
71+
```
72+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
```java
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode() {}
9+
* TreeNode(int val) { this.val = val; }
10+
* TreeNode(int val, TreeNode left, TreeNode right) {
11+
* this.val = val;
12+
* this.left = left;
13+
* this.right = right;
14+
* }
15+
* }
16+
*/
17+
class Solution {
18+
private void inOrder(TreeNode root, ArrayList<Integer>ans){
19+
if(root==null) return ;
20+
21+
inOrder(root.left,ans);
22+
ans.add(root.val);
23+
inOrder(root.right,ans);
24+
}
25+
public boolean findTarget(TreeNode root, int k) {
26+
ArrayList<Integer> ans = new ArrayList<>();
27+
inOrder(root,ans);
28+
29+
int s =0;
30+
int e = ans.size()-1;
31+
32+
while(s<e){
33+
int sum = ans.get(s) + ans.get(e);
34+
if(sum==k) return true;
35+
else if(sum<k) s++;
36+
else e--;
37+
}
38+
return false;
39+
}
40+
41+
}
42+
43+
```
44+
45+
```java
46+
/**
47+
* Definition for a binary tree node.
48+
* public class TreeNode {
49+
* int val;
50+
* TreeNode left;
51+
* TreeNode right;
52+
* TreeNode() {}
53+
* TreeNode(int val) { this.val = val; }
54+
* TreeNode(int val, TreeNode left, TreeNode right) {
55+
* this.val = val;
56+
* this.left = left;
57+
* this.right = right;
58+
* }
59+
* }
60+
*/
61+
class Solution {
62+
HashSet<Integer> set = new HashSet<>();
63+
public boolean findTarget(TreeNode root, int k) {
64+
if(root==null) return false;
65+
if(set.contains(k-root.val)){
66+
return true;
67+
}
68+
else set.add(root.val);
69+
70+
return findTarget(root.left,k) || findTarget(root.right,k);
71+
72+
}
73+
74+
}
75+
```

0 commit comments

Comments
 (0)