|
| 1 | + |
| 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 | + |
| 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 :) |
0 commit comments