Use each character as pivot and check for palindrome in both direction. Use HashMap to save unique Palanidrom


  • 0
    J

    Re: [Given a string of lowercase ASCII characters](find all distinct continuous palindromic sub-strings of it.)

    Here is another solution using pivot each character of string and expend both direction to collect its palindrome part for both even and odd number, Use HashMap to save unique palindrome

    /*
    Given a string of lowercase ASCII characters, find all distinct continuous palindromic sub-strings of it.
    /
    /

    Algo : Iterate over each character and take it as pivot and expend its in bothdirection to found if string become palindrom or not . Use hashMap to find
    unique palindrom

    */

    func findSetofPalaindrom(s []byte) {
    result := make(map[string]bool, 0)

        for i := 0; i < len(s); i++ {
                //Even length palindrom
                isPalindrom(result, s, i, i+1)
                //Odd length Palaindron
                isPalindrom(result, s, i, i)
        }
        for v, _ := range result {
                fmt.Println(v)
        }
    

    }
    func isPalindrom(r map[string]bool, s []byte, i, j int) {
    for i >= 0 && j < len(s) && (s[i] == s[j]) {
    s1 := string(s[i : j+1])
    _, ok := r[s1]
    if !ok {
    r[s1] = true
    }
    i--
    j++
    }
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.