-1

the function of code is to find a palindrome substring at any index. code is running well and having no errors but the main problem is that it took time more than it specified. i tried every possible action but couldn't get the desired execution time

String palindrome(String s) throws Exception  {

        int x =0,y=0;
        
        int max=0;
      


        for (int i = 0,k=1; i < str.length() && k<=str.length();k++) {
                String s = str.substring(i,k);

                
                StringBuilder sb = new StringBuilder(s);
                sb.reverse();
                String reverse = sb.toString();

                if (s.equals(reverse)) {

                    if (s.length() > max) {

                        max = s.length();
                        x = i;
                    y = k;
                    }

                }

            if (k==str.length()) {
                ++i;
                k=i;
            }



            }

        return str.substring(x,y);
}

the function of code is to find a palindrome substring at any index. code is running well and having no errors but the main problem is that it took time more than it specified. i tried every possible action but couldn't get the desired execution time

Progman
  • 16,827
  • 6
  • 33
  • 48
  • one suggestion you are technically using two for loops by making modifications in one. this is not helping you in optimization or in any sense. also, it makes debugging your code difficult. restrain from this type of practice. – Kakarot Feb 12 '23 at 04:45
  • Formal parameter of type `String` should be named `str` - the posted code hides the parameter name `s` and likely does not compile. – Computable Feb 12 '23 at 06:52

2 Answers2

0

You can reduce the running time by memorizing the solutions to the smaller problems. I don’t have Java readymade code for you, but I’ll give you the outline of the algorithm in Python that you can use. It is trivial to convert this code to Java.

dp[i][j] = true if s[i:j+1] is a palindrome.
// Start from the end and decrement, since dp[start + 1]  must be known in order to calculate dp[start].
// Similarly, end is incremented since end - 1 must be known in order to calculate dp[start][end].
for start in range(n - 1, -1, -1):
    for end in range(start + 1, n):
        if s[start] == s[end]:
            palindrome_len = end - start + 1
            // Either it’s a 2-character palindrome or the remaining string is a palindrome.
            dp[start][end] = palindrome_len == 2 or dp[start + 1][end - 1]
            if dp[start][end]:
                count += 1
Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
0

You are using brute force which tends to have a time limit problem because it's using two for a loop. if there are very large strings let's say (10^6) it will timeout in any online contest or editor.

There is another way to solve this you can use DP memoization. for ex - when you are calculating (i,j) substring is palindrome or not. you can calculate first the smaller substring so that you know whether (i-1,j-1) are palindromes or not if they are and s.charAt(i)==s.charAt(j) then (i,j) will also be a palindrome.

Algorithm

  • start picking 2 sizes substring
  • then check for 3 using the already stored 2-size substring.
  • go on till size exceeds the length of string.
Kakarot
  • 195
  • 2
  • 10