Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 72e72e6

Browse files
committed
Linked List problems
1 parent cbdd771 commit 72e72e6

8 files changed

+203
-10
lines changed
165 KB
Loading

src/main/images/WildCardMatching.png

243 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
image::../../../images/SearchInRotatedSortedArray.png[]

src/main/java/com/arrays/SearchInRotatedSortedArray_BitonicArray.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* An array is called Bitonic if it is comprises of an increasing sequence of integers followed immediately
55
* by a decreasing sequence of integers.
66
* Given a bitonic array A of N distinct integers. Find a element X in it.
7+
* <p>
8+
* You may assume no duplicate exists in the array.
79
*/
810
public class SearchInRotatedSortedArray_BitonicArray {
911

@@ -13,32 +15,35 @@ public static int search(int[] nums, int target) {
1315

1416
while (left <= right) {
1517

16-
int mid = left + (right - left) / 2;
18+
int mid = (left + right) / 2;
1719
if (target == nums[mid])
1820
return mid;
1921

20-
if (nums[left] <= nums[mid]) {
21-
if (nums[left] <= target && target < nums[mid]) {
22+
/** update left and right
23+
left will always move forward and right will always shrink*/
24+
if (nums[left] <= nums[mid]) { /** first half = left -> mid */
25+
if (nums[left] <= target && target < nums[mid]) { /** target is between left and mid so means left at right position and need
26+
to update right as right will always shrink/move backward it will be mid-1 */
2227
right = mid - 1;
2328
} else {
2429
left = mid + 1;
2530
}
26-
}
27-
28-
if (nums[mid] <= nums[right]) {
29-
if (nums[mid] < target && target <= nums[right]) {
31+
} else { /** second half = mid -> right */
32+
if (nums[mid] < target && target <= nums[right]) { /** target is between mid and right so means right is at correct position and need
33+
to update left as left will always move forward it will be mid+1 */
3034
left = mid + 1;
3135
} else {
3236
right = mid - 1;
3337
}
3438
}
39+
3540
}
3641

3742
return -1;
3843
}
3944

4045
public static void main(String args[]) {
4146
int[] arr = {4, 5, 6, 7, 0, 1, 2};
42-
System.out.println("Element found at index : " + search(arr, 6));
47+
System.out.println("Element found at index : " + search(arr, 7));
4348
}
4449
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
image::../../../images/WildCardMatching.png[]

src/main/java/com/arrays/WildCardMatching.html

Lines changed: 109 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.arrays;
2+
3+
/**
4+
5+
'?' Matches any single character.
6+
'*' Matches any sequence of characters (including the empty sequence).
7+
8+
Input:
9+
s = "cb"
10+
p = "?a"
11+
Output: false
12+
Explanation: '?' matches 'c', but the second letter is 'a', which does not match 'b'.
13+
14+
Time complexity of above solution is O(m x n). Auxiliary space used is also O(m x n).
15+
where m = length of pattern
16+
n = length of String
17+
18+
*/
19+
20+
public class WildCardMatching {
21+
22+
public boolean isMatch(String s, String p) {
23+
char[] str = s.toCharArray();
24+
char[] pattern = p.toCharArray();
25+
26+
//replace multiple * with one *
27+
//e.g a**b***c --> a*b*c
28+
int writeIndex = 0;
29+
boolean isFirst = true;
30+
for (int i = 0; i < pattern.length; i++) {
31+
if (pattern[i] == '*') {
32+
if (isFirst) {
33+
pattern[writeIndex++] = pattern[i];
34+
isFirst = false;
35+
}
36+
} else {
37+
pattern[writeIndex++] = pattern[i];
38+
isFirst = true;
39+
}
40+
}
41+
42+
boolean T[][] = new boolean[str.length + 1][pattern.length + 1];
43+
44+
if (writeIndex > 0 && pattern[0] == '*') {
45+
T[0][1] = true;
46+
}
47+
48+
// base case
49+
T[0][0] = true;
50+
51+
/**
52+
* Rules
53+
* 1) if the value at boolean_array[i][j] is a matching character or a “?”.
54+
* boolean_array[i][j] = boolean_array[i-1][j-1] (means diagonal)
55+
*
56+
* 2) if the value at at boolean_array[i][j] is “*”.
57+
* boolean_array[i][j] = boolean_array[i][j-1] || boolean_array[i-1][j] (means one one left or one on top whichever is true)
58+
*/
59+
60+
for (int i = 1; i <= str.length; i++)
61+
{
62+
for (int j = 1; j <= pattern.length; j++)
63+
if (pattern[j - 1] == '?' || str[i - 1] == pattern[j - 1]) {
64+
T[i][j] = T[i - 1][j - 1]; //diagonal
65+
} else if (pattern[j - 1] == '*') {
66+
T[i][j] = T[i - 1][j] || T[i][j - 1];
67+
}
68+
}
69+
70+
return T[str.length][pattern.length];
71+
}
72+
73+
74+
public static void main(String args[]) {
75+
WildCardMatching wcm = new WildCardMatching();
76+
System.out.println(wcm.isMatch("xbylmz", "x?y*z"));
77+
}
78+
}

src/main/java/com/theory/UsefulLinks

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ https://medium.com/@ajinkyajawale/inorder-preorder-postorder-traversal-of-binary
99

1010
GitHub
1111
https://github.com/kdn251/interviews
12-
https://github.com/mission-peace/interview/tree/master/src/com/interview/
13-
https://github.com/mission-peace/interview/wiki/String
12+
https://github.com/mission-peace/interview/wiki/String - Tushar Roy
1413
https://github.com/varunu28/InterviewBit-Java-Solutions
1514
https://github.com/dennyzhang/code.dennyzhang.com/tree/master/problems
1615
https://github.com/yc-gh/GeeksForGeeks

0 commit comments

Comments
 (0)