Skip to content

Commit 1f854e5

Browse files
author
Prashant Jain
committed
Adding Stack & Strings | Formatting
1 parent ded7b99 commit 1f854e5

11 files changed

Lines changed: 217 additions & 107 deletions

File tree

src/in/knowledgegate/dsa/array/MaximumSubArray.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,19 @@
11
package in.knowledgegate.dsa.array;
22

33
/**
4-
* Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
5-
* A subarray is a contiguous part of an array.
6-
*
7-
* Example 1
8-
* Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
9-
* Output: 6
10-
* Explanation: [4,-1,2,1] has the largest sum = 6.
11-
*
12-
* Example 2:
13-
* Input: nums = [1]
14-
* Output: 1
15-
*
16-
* Example 3:
17-
* Input: nums = [5,4,-1,7,8]
18-
* Output: 23
19-
*
20-
*
21-
* Constraints:
22-
* 1 <= nums.length <= 105
23-
* -104 <= nums[i] <= 104
4+
* Given an integer array nums, find the contiguous subarray (containing at
5+
* least one number) which has the largest sum and return its sum. A subarray is
6+
* a contiguous part of an array.
7+
* <p>
8+
* Example 1 Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation:
9+
* [4,-1,2,1] has the largest sum = 6.
10+
* <p>
11+
* Example 2: Input: nums = [1] Output: 1
12+
* <p>
13+
* Example 3: Input: nums = [5,4,-1,7,8] Output: 23
14+
* <p>
15+
* <p>
16+
* Constraints: 1 <= nums.length <= 105 -104 <= nums[i] <= 104
2417
*/
2518
public class MaximumSubArray {
2619
public int maxSubArray(int[] nums) {

src/in/knowledgegate/dsa/array/RemoveDuplicatesSortedArray.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
package in.knowledgegate.dsa.array;
22

33
/**
4-
* Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.
5-
* Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.
6-
* Return k after placing the final result in the first k slots of nums.
7-
*
8-
* Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
9-
*
10-
* Example 1:
11-
* Input: nums = [1,1,2]
12-
* Output: 2, nums = [1,2,_]
13-
* Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
14-
* It does not matter what you leave beyond the returned k (hence they are underscores).
15-
*
16-
* Example 2:
17-
* Input: nums = [0,0,1,1,1,2,2,3,3,4]
18-
* Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
19-
* Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
20-
* It does not matter what you leave beyond the returned k (hence they are underscores).
21-
*
22-
*
23-
* Constraints:
24-
* 1 <= nums.length <= 3 * 104
25-
* -100 <= nums[i] <= 100
26-
* nums is sorted in non-decreasing order.
4+
* Given an integer array nums sorted in non-decreasing order, remove the
5+
* duplicates in-place such that each unique element appears only once. The
6+
* relative order of the elements should be kept the same. Since it is
7+
* impossible to change the length of the array in some languages, you must
8+
* instead have the result be placed in the first part of the array nums. More
9+
* formally, if there are k elements after removing the duplicates, then the
10+
* first k elements of nums should hold the final result. It does not matter
11+
* what you leave beyond the first k elements. Return k after placing the final
12+
* result in the first k slots of nums.
13+
* <p>
14+
* Do not allocate extra space for another array. You must do this by modifying
15+
* the input array in-place with O(1) extra memory.
16+
* <p>
17+
* Example 1: Input: nums = [1,1,2] Output: 2, nums = [1,2,_] Explanation: Your
18+
* function should return k = 2, with the first two elements of nums being 1 and
19+
* 2 respectively. It does not matter what you leave beyond the returned k
20+
* (hence they are underscores).
21+
* <p>
22+
* Example 2: Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums =
23+
* [0,1,2,3,4,_,_,_,_,_] Explanation: Your function should return k = 5, with
24+
* the first five elements of nums being 0, 1, 2, 3, and 4 respectively. It does
25+
* not matter what you leave beyond the returned k (hence they are
26+
* underscores).
27+
* <p>
28+
* <p>
29+
* Constraints: 1 <= nums.length <= 3 * 104 -100 <= nums[i] <= 100 nums is
30+
* sorted in non-decreasing order.
2731
*/
2832
public class RemoveDuplicatesSortedArray {
2933
public int removeDuplicates(int[] nums) {

src/in/knowledgegate/dsa/array/SearchInsertPosition.java

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
11
package in.knowledgegate.dsa.array;
22

33
/**
4-
* Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
5-
* You must write an algorithm with O(log n) runtime complexity.
6-
*
7-
*
8-
* Example 1:
9-
* Input: nums = [1,3,5,6], target = 5
10-
* Output: 2
11-
*
12-
* Example 2:
13-
* Input: nums = [1,3,5,6], target = 2
14-
* Output: 1
15-
*
16-
* Example 3:
17-
* Input: nums = [1,3,5,6], target = 7
18-
* Output: 4
19-
*
20-
*
21-
* Constraints:
22-
* 1 <= nums.length <= 104
23-
* -104 <= nums[i] <= 104
24-
* nums contains distinct values sorted in ascending order.
25-
* -104 <= target <= 104
4+
* Given a sorted array of distinct integers and a target value, return the
5+
* index if the target is found. If not, return the index where it would be if
6+
* it were inserted in order. You must write an algorithm with O(log n) runtime
7+
* complexity.
8+
* <p>
9+
* <p>
10+
* Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2
11+
* <p>
12+
* Example 2: Input: nums = [1,3,5,6], target = 2 Output: 1
13+
* <p>
14+
* Example 3: Input: nums = [1,3,5,6], target = 7 Output: 4
15+
* <p>
16+
* <p>
17+
* Constraints: 1 <= nums.length <= 104 -104 <= nums[i] <= 104 nums contains
18+
* distinct values sorted in ascending order. -104 <= target <= 104
2619
*/
2720
public class SearchInsertPosition {
2821
public int searchInsert(int[] nums, int target) {
@@ -31,7 +24,7 @@ public int searchInsert(int[] nums, int target) {
3124
int mid = beg + (end - beg) / 2;
3225
if (nums[mid] == target) {
3326
return mid;
34-
} else if(nums[mid] > target) {
27+
} else if (nums[mid] > target) {
3528
end = mid - 1;
3629
} else {
3730
beg = mid + 1;

src/in/knowledgegate/dsa/array/TwoSum.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,21 @@
44
import java.util.Map;
55

66
/**
7-
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
8-
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
* Given an array of integers nums and an integer target, return indices of the
8+
* two numbers such that they add up to target. You may assume that each input
9+
* would have exactly one solution, and you may not use the same element twice.
910
* You can return the answer in any order.
10-
*
11-
* Example 1:
12-
* Input: nums = [2,7,11,15], target = 9
13-
* Output: [0,1]
14-
* Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
15-
*
16-
* Example 2:
17-
* Input: nums = [3,2,4], target = 6
18-
* Output: [1,2]
19-
*
20-
* Example 3:
21-
* Input: nums = [3,3], target = 6
22-
* Output: [0,1]
23-
*
24-
*
25-
* Constraints:
26-
* 2 <= nums.length <= 104
27-
* -109 <= nums[i] <= 109
28-
* -109 <= target <= 109
29-
* Only one valid answer exists.
11+
* <p>
12+
* Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation:
13+
* Because nums[0] + nums[1] == 9, we return [0, 1].
14+
* <p>
15+
* Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2]
16+
* <p>
17+
* Example 3: Input: nums = [3,3], target = 6 Output: [0,1]
18+
* <p>
19+
* <p>
20+
* Constraints: 2 <= nums.length <= 104 -109 <= nums[i] <= 109 -109 <= target <=
21+
* 109 Only one valid answer exists.
3022
*/
3123
public class TwoSum {
3224
public int[] twoSum(int[] nums, int target) {

src/in/knowledgegate/dsa/complexity/ArrayCopy.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
public class ArrayCopy {
44

55
public static void main(String[] args) {
6-
int[] newCopy = copy(new int[]{1, 2, 3 , 4, 5});
6+
int[] newCopy = copy(new int[]{1, 2, 3, 4, 5});
77
}
88

9-
private static int[] copy (int []array) {
10-
int newCopy[] = new int[array.length];
9+
private static int[] copy(int[] array) {
10+
int[] newCopy = new int[array.length];
1111
for (int i = 0; i < array.length; i++) {
1212
newCopy[i] = array[i];
1313
}

src/in/knowledgegate/dsa/complexity/ArraySum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static void main(String[] args) {
66
System.out.println("Sum:" + sum(new int[]{1, 2, 3, 4, 5}));
77
}
88

9-
private static int sum (int []array) {
9+
private static int sum(int[] array) {
1010
int result = 0;
1111
for (int i = 0; i < array.length; i++) {
1212
result += array[i];

src/in/knowledgegate/dsa/complexity/Sum.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public static void main(String[] args) {
66
System.out.println("Sum:" + sum(8, 10));
77
}
88

9-
private static int sum (int a, int b) {
9+
private static int sum(int a, int b) {
1010
return a + b;
1111
}
1212
}

src/in/knowledgegate/dsa/linkedlist/PascalTriangle.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44
import java.util.List;
55

66
/**
7-
* Given an integer numRows, return the first numRows of Pascal's triangle.
8-
* In Pascal's triangle, each number is the sum of the two numbers directly above it as shown.
9-
*
10-
* Example 1:
11-
* Input: numRows = 5
12-
* Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
13-
*
14-
* Example 2:
15-
* Input: numRows = 1
16-
* Output: [[1]]
17-
*
18-
*
19-
* Constraints:
20-
* 1 <= numRows <= 30
7+
* Given an integer numRows, return the first numRows of Pascal's triangle. In
8+
* Pascal's triangle, each number is the sum of the two numbers directly above
9+
* it as shown.
10+
* <p>
11+
* Example 1: Input: numRows = 5 Output:
12+
* [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
13+
* <p>
14+
* Example 2: Input: numRows = 1 Output: [[1]]
15+
* <p>
16+
* <p>
17+
* Constraints: 1 <= numRows <= 30
2118
*/
2219
public class PascalTriangle {
2320
public List<List<Integer>> generate(int numRows) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package in.knowledgegate.dsa.stack;
2+
3+
import java.util.Stack;
4+
5+
/**
6+
* Given a string s containing just the characters '(', ')', '{', '}', '[' and
7+
* ']', determine if the input string is valid. An input string is valid if:
8+
* <p>
9+
* Open brackets must be closed by the same type of brackets. Open brackets must
10+
* be closed in the correct order.
11+
* <p>
12+
* <p>
13+
* Example 1: Input: s = "()" Output: true
14+
* <p>
15+
* Example 2: Input: s = "()[]{}" Output: true
16+
* <p>
17+
* Example 3: Input: s = "(]" Output: false
18+
* <p>
19+
* Constraints: 1 <= s.length <= 104 s consists of parentheses only '()[]{}'.
20+
*/
21+
public class ValidParenthesis {
22+
public boolean isValid(String s) {
23+
Stack<Character> stack = new Stack<>();
24+
for (int i = 0; i < s.length(); i++) {
25+
char c = s.charAt(i);
26+
switch (c) {
27+
case '{':
28+
case '[':
29+
case '(':
30+
stack.push(c);
31+
break;
32+
case '}':
33+
if (stack.isEmpty() || '{' != stack.pop()) {
34+
return false;
35+
}
36+
break;
37+
case ']':
38+
if (stack.isEmpty() || '[' != stack.pop()) {
39+
return false;
40+
}
41+
break;
42+
case ')':
43+
if (stack.isEmpty() || '(' != stack.pop()) {
44+
return false;
45+
}
46+
break;
47+
}
48+
}
49+
return stack.isEmpty();
50+
}
51+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package in.knowledgegate.dsa.strings;
2+
3+
/**
4+
* Write a function to find the longest common prefix string amongst an array of
5+
* strings. If there is no common prefix, return an empty string "".
6+
* <p>
7+
* Example 1: Input: strs = ["flower","flow","flight"] Output: "fl"
8+
* <p>
9+
* Example 2: Input: strs = ["dog","racecar","car"] Output: "" Explanation:
10+
* There is no common prefix among the input strings.
11+
* <p>
12+
* <p>
13+
* Constraints: 1 <= strs.length <= 200 0 <= strs[i].length <= 200 strs[i]
14+
* consists of only lower-case English letters.
15+
*/
16+
public class LongestCommonPrefix {
17+
public String longestCommonPrefix(String[] strs) {
18+
String result = "";
19+
for (int i = 0; i < strs[0].length(); i++) {
20+
for (int j = 1; j < strs.length; j++) {
21+
if (strs[j].length() <= i || strs[0].charAt(i) != strs[j].charAt(i)) {
22+
return result;
23+
}
24+
}
25+
result += strs[0].charAt(i);
26+
}
27+
return result;
28+
}
29+
}

0 commit comments

Comments
 (0)