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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,987 questions

51,931 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 140 views
1 answer 177 views
1 answer 155 views
1 answer 153 views
1 answer 143 views
1 answer 162 views
1 answer 158 views
...