17

I have the following function to get all of the substrings from a string in JavaScript. I know it's not correct but I feel like I am going about it the right way. Any advice would be great.

 var theString     = 'somerandomword',
     allSubstrings = []; 

getAllSubstrings(theString);

function getAllSubstrings(str) {

  var start = 1;

  for ( var i = 0; i < str.length; i++  ) {

     allSubstrings.push( str.substring(start,i) ); 

  }

} 

console.log(allSubstrings)

Edit: Apologies if my question is unclear. By substring I mean all combinations of letters from the string (do not have to be actual words) So if the string was 'abc' you could have [a, ab, abc, b, ba, bac etc...] Thank you for all the responses.

user2085143
  • 4,162
  • 7
  • 39
  • 68
  • 3
    What do you mean by "all" the substrings? – Evan Trimboli Nov 26 '16 at 13:24
  • What output you expect? – Aruna Nov 26 '16 at 13:28
  • This should be useful http://stackoverflow.com/questions/4547609/how-do-you-get-a-string-to-a-character-array-in-javascript – Omid Farvid Nov 26 '16 at 13:28
  • It is unclear what you mean with 'substrings'. If you're trying to get each word as its own value you would have to split the string specifically at A, B, C. But what if the word is not 'somerandomword'? They you would either have to use a dictionary or another array of words you would like your function to split by. Each of these words cannot be defined as a 'word'. – Tommie Nov 26 '16 at 13:55
  • I wonder, is 'bac' a substring of 'abc' ?! – EhsanT Nov 26 '16 at 16:49
  • What would be a better way to phrase this question and i'll update it. – user2085143 Nov 26 '16 at 16:51
  • Your new edit does clarify things. I'm curious though -- why would you would want to do this? :) – Tommie Nov 26 '16 at 19:54

7 Answers7

34

You need two nested loop for the sub strings.

function getAllSubstrings(str) {
  var i, j, result = [];

  for (i = 0; i < str.length; i++) {
      for (j = i + 1; j < str.length + 1; j++) {
          result.push(str.slice(i, j));
      }
  }
  return result;
}

var theString = 'somerandomword';
console.log(getAllSubstrings(theString));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
3

A modified version of Accepted Answer. In order to give the minimum string length for permutation

function getAllSubstrings(str, size) {
  var i, j, result = [];
  size = (size || 0);
  for (i = 0; i < str.length; i++) {
    for (j = str.length; j - i >= size; j--) {
      result.push(str.slice(i, j));
    }
  }
  return result;
}

var theString = 'somerandomword';
console.log(getAllSubstrings(theString, 6));
codejockie
  • 9,020
  • 4
  • 40
  • 46
vineetv2821993
  • 927
  • 12
  • 25
2

Below is a recursive solution to the problem

let result = [];

function subsetsOfString(str, curr = '', index = 0) {
  if (index == str.length) {
    result.push(curr);
    return result;
  }
  subsetsOfString(str, curr, index + 1);
  subsetsOfString(str, curr + str[index], index + 1);
}

subsetsOfString("somerandomword");
console.log(result);
codejockie
  • 9,020
  • 4
  • 40
  • 46
0

An answer with the use of substring function.

function getAllSubstrings(str) {
  var res = [];
  for (let i = 0; i < str.length; i++) {
    for (let j = i + 1; j <= str.length; j++) {
      res.push(str.substring(i, j));
    }
  }
  return res;
}
var word = "randomword";
console.log(getAllSubstrings(word));
0

Below is a simple approach to find all substrings


    var arr = "abcde";

     for(let i=0; i < arr.length; i++){

      for(let j=i; j < arr.length; j++){

      let bag ="";
      for(let k=i; k<j; k++){
       bag = bag + arr[k]
    }
    console.log(bag)
  }
}
0
function generateALlSubstrings(N,str){
    
    for(let i=0; i<N; i++){

        for(let j=i+1; j<=N; j++){

            console.log(str.substring(i, j));
        }
    }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
-1
function getSubstrings(s){
    //if string passed is null or undefined or empty string
    if(!s)   return []; 

    let substrings = [];

    for(let length = 1 ; length <= s.length; length++){
        for(let  i = 0 ; (i + length) <= s.length ; i++){
            substrings.push(s.substr(i, length));
        }
    }

    return substrings;
}
Prksh
  • 1