Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,037 questions

40,846 answers

573 users

How to get all palindrome substrings in a string with JavaScript

1 Answer

0 votes
function reverse_string(s) {
    return s.split("").reverse().join("");
}
    
function is_palindrome(s, i, j) {
    var rev = s.substring(i, j); 
    rev = reverse_string(rev);
            
    var tmp_s = s.substring(i, j); 
    var b = false;
    if (tmp_s === rev && tmp_s.length >= 2) {
        b = true;
    }
    
    return b;
}

var s = "abaab";

for (var i = 0; i < s.length; i++) { 
    for (var j = i + 1; j <= s.length; j++) { 
        if (is_palindrome(s, i, j)) {
            document.write(s.substring(i, j) + "<br />");
        }
    }
}
 


/*

aba
baab
aa
    
*/

 





answered Oct 25, 2019 by avibootz

Related questions

1 answer 82 views
1 answer 83 views
1 answer 107 views
1 answer 93 views
1 answer 77 views
...