Skip to content

Commit 6e0dd34

Browse files
committed
Remove conflicts
1 parent e736b26 commit 6e0dd34

File tree

6 files changed

+170
-3
lines changed

6 files changed

+170
-3
lines changed

Day2/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,3 @@ def palindrome(str)
682682
str != nil and str === short_solution(str)
683683
end
684684
```
685-
686-
687-

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Read [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
2525
6. [Day 6 -- Sentence Cap + Word Reversal + Anagram](./day6/) -- [http://codetoexpress.tech/dc/day6/](http://codetoexpress.tech/dc/day6/)
2626
7. [Day 7 -- One Edit Away](./day7/) -- [http://codetoexpress.tech/dc/day7/](http://codetoexpress.tech/dc/day7/)
2727
8. [Day 8 -- Minimum Edit Distance](./day8/) -- [http://codetoexpress.tech/dc/day8/](http://codetoexpress.tech/dc/day8/)
28+
9. [Day 9 -- Smallest Substring Problem](./day9/) -- [http://codetoexpress.tech/dc/day9/](http://codetoexpress.tech/dc/day9/)
2829

2930
## Timeline
3031

day9/JavaScript/sol1.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @author MadhavBahlMD
3+
* @date 01/01/2019
4+
* METHOD (Using Array)
5+
* - Initalize an empty array `strArr` that will store all the substrings that contains all the array elements
6+
* - iterate over each character of string and check whether it is present in the array
7+
* - If it is not present in the array, then continue, else duplicate the array into another temporary array `tempArr` and remove the current letter from the temp arr
8+
* - Take another variable j, and iterate over the rest of the string till the last remaining element in the tempArr, and keep appending each character in some temporary strinig.
9+
* - Stop the iteration if either it is the end of the string, or tempArr is empty
10+
* - if the current (temporary) string contains all elements of the array, store push it to `strArr`
11+
* - After the iterations are complete
12+
*/
13+
14+
function smallestSubstr (str, arr) {
15+
let strArr = [],
16+
strLen = str.length;
17+
18+
// iterate over the string to find any match
19+
for (let i=0; i<strLen; i++) {
20+
// Check whether the current letter exists in array
21+
let pos = arr.indexOf(str[i]);
22+
// if there is only one character in the array return that as the smallest substring
23+
if (arr.length === 1 && str[i] === arr[0]) return str[i];
24+
25+
// Check for the remaining characters
26+
if (pos >= 0) {
27+
// Duplicate the arr (To Prevent Shallow Copy)
28+
let tempArr = JSON.parse(JSON.stringify(arr));
29+
30+
// Append to a temporary string and remove that element from tempArr
31+
let currentStr = tempArr[pos];
32+
tempArr.splice (pos, 1);
33+
34+
// Iterate over the remaining string elements
35+
let j=i+1, flag = 0;
36+
while (j<strLen) {
37+
let nextPos = tempArr.indexOf(str[j]);
38+
currentStr += str[j];
39+
if (nextPos >= 0) {
40+
tempArr.splice (nextPos, 1);
41+
}
42+
if (tempArr.length<=0) {
43+
flag = 1;
44+
break;
45+
}
46+
j++;
47+
}
48+
49+
if (flag === 1) strArr.push (currentStr);
50+
}
51+
}
52+
53+
// Return empty string if strArr is empty
54+
if (strArr.length === 0) return "";
55+
56+
// find the minimum index in cntArr
57+
let minCntIndex = 0;
58+
for (let i=1; i<strArr.length; i++) {
59+
if (strArr[minCntIndex].length > strArr[i].length) {
60+
minCntIndex = i;
61+
}
62+
}
63+
64+
return (strArr[minCntIndex]);
65+
}
66+
67+
console.log (smallestSubstr ('abyuxabyteqaebczt', ['a','b','c']));
68+
console.log (smallestSubstr ("abd", ['x','y','z']));
69+
console.log (smallestSubstr ("x", ['x']));
70+
console.log (smallestSubstr ("afekbtcodebancfeger", ["a","b","c"]));

day9/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
![cover](./cover.png)
2+
3+
# Day 9 - Smallest Substring Problem
4+
5+
**Question** -- Given an array of unique characters and a string, write a program to find the smallest substring of the given string which contains all the characters of the array.
6+
7+
**Example**
8+
9+
```
10+
input:
11+
arr = [a, b, c]
12+
str = "abyuxabyteqaebczt"
13+
output: "aebc"
14+
```
15+
16+
![ques](./ques.png)
17+
18+
## JavaScript Implementation
19+
20+
### [Solution 1](./JavaScript/sol1.js)
21+
22+
```js
23+
/**
24+
* @author MadhavBahlMD
25+
* @date 01/01/2019
26+
* METHOD (Using Array)
27+
* - Initalize an empty array `strArr` that will store all the substrings that contains all the array elements
28+
* - iterate over each character of string and check whether it is present in the array
29+
* - If it is not present in the array, then continue, else duplicate the array into another temporary array `tempArr` and remove the current letter from the temp arr
30+
* - Take another variable j, and iterate over the rest of the string till the last remaining element in the tempArr, and keep appending each character in some temporary strinig.
31+
* - Stop the iteration if either it is the end of the string, or tempArr is empty
32+
* - if the current (temporary) string contains all elements of the array, store push it to `strArr`
33+
* - After the iterations are complete
34+
*/
35+
36+
function smallestSubstr (str, arr) {
37+
let strArr = [],
38+
strLen = str.length;
39+
40+
// iterate over the string to find any match
41+
for (let i=0; i<strLen; i++) {
42+
// Check whether the current letter exists in array
43+
let pos = arr.indexOf(str[i]);
44+
// if there is only one character in the array return that as the smallest substring
45+
if (arr.length === 1 && str[i] === arr[0]) return str[i];
46+
47+
// Check for the remaining characters
48+
if (pos >= 0) {
49+
// Duplicate the arr (To Prevent Shallow Copy)
50+
let tempArr = JSON.parse(JSON.stringify(arr));
51+
52+
// Append to a temporary string and remove that element from tempArr
53+
let currentStr = tempArr[pos];
54+
tempArr.splice (pos, 1);
55+
56+
// Iterate over the remaining string elements
57+
let j=i+1, flag = 0;
58+
while (j<strLen) {
59+
let nextPos = tempArr.indexOf(str[j]);
60+
currentStr += str[j];
61+
if (nextPos >= 0) {
62+
tempArr.splice (nextPos, 1);
63+
}
64+
if (tempArr.length<=0) {
65+
flag = 1;
66+
break;
67+
}
68+
j++;
69+
}
70+
71+
if (flag === 1) strArr.push (currentStr);
72+
}
73+
}
74+
75+
// Return empty string if strArr is empty
76+
if (strArr.length === 0) return "";
77+
78+
// find the minimum index in cntArr
79+
let minCntIndex = 0;
80+
for (let i=1; i<strArr.length; i++) {
81+
if (strArr[minCntIndex].length > strArr[i].length) {
82+
minCntIndex = i;
83+
}
84+
}
85+
86+
return (strArr[minCntIndex]);
87+
}
88+
89+
console.log (smallestSubstr ('abyuxabyteqaebczt', ['a','b','c']));
90+
console.log (smallestSubstr ("abd", ['x','y','z']));
91+
console.log (smallestSubstr ("x", ['x']));
92+
console.log (smallestSubstr ("afekbtcodebancfeger", ["a","b","c"]));
93+
```
94+
95+
### Have Another solution?
96+
97+
The beauty of programming lies in the fact that there is never a single solution to any problem.
98+
99+
In case you have an alternative way to solve this problem, do contribute to this repository :)

day9/cover.png

143 KB
Loading

day9/ques.png

798 KB
Loading

0 commit comments

Comments
 (0)