LeetCode 05 longest palindrome substring

Title description

description:

Given a string s, find the longest palindrome substring in s. You can assume that the maximum length of s is 1000.

Example 1:

Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"
Output: "bb"

Ordinary violence

analysis:

  • Find the longest palindrome string. There are two forms of palindrome string, odd number string and even number string. We only need to enumerate the situation from left to right, and then return the longest string.
  • When writing code, pay attention to the problem of borders not to cross the border. Return a reasonably numbered string.
  • Do not use the String type to piece together, because String is an immutable class. Each piece together will generate a new string, and multiple pieces together will cause very low efficiency.

Pass code:

public String longestPalindrome(String s) {
    
    
		int max = 0;
		String va = "";
		if (s.length() > 0)
			va = s.charAt(0) + "";
		for (int i = 0; i < s.length() - 1; i++) {
    
    
			int l = i, r = i;//奇数个回文串
			while (l >= 0 && r <= s.length() - 1) {
    
    
				if (s.charAt(l) == s.charAt(r)) {
    
    
					l--;
					r++;
				} else {
    
    
					break;
				}
			}
			if (r - l + 1 > max) {
    
    
				max = r - l + 1;
				va = s.substring(l+1, r );
			}
			l = i;r = i + 1;//偶数个回文串
			if (s.charAt(i) == s.charAt(i + 1)) {
    
    
				while (l >= 0 && r <= s.length() - 1) {
    
    
					if (s.charAt(l) == s.charAt(r)) {
    
    
						l--;
						r++;
					} else {
    
    
						break;
					}
				}
			}
			if (r - l + 1 > max) {
    
    
				max = r - l + 1;
				va = s.substring(l+1, r );
			}

		}
		return va;
	}

Center spread

Can there be any optimized plan to find the longest palindrome string ?

First, where is the longest possible occurrence?

  • Of course, the longest will appear in the middle position:

Insert picture description here
If the maximum length is found the first time, do you need to find other palindrome strings that cannot be longer than it ?

  • Of course not.

What method can be used to determine that there is no need to search for a shorter palindrome?

  • Search from the middle to both sides, and the length of the largest palindrome is max.
  • If two times the distance from the point to one of the boundaries is obviously less than the max length of the longest palindrome when spreading to both sides, there is no need to calculate. You can stop directly.

Insert picture description here

However, in terms of specific code implementation, some limits and special circumstances should be paid attention to. The ac code is:

// 法二,中间扩散
	public static String getmaxhuiwen(int l,int r,String s) {
    
    
		if(l>r)return"";
		while (l >= 0 && r <= s.length() - 1) {
    
    
			if (s.charAt(l) == s.charAt(r)) {
    
    
				l--;
				r++;
			} else {
    
    
				break;
			}
		}
		return s.substring(l+1, r);
		
	}
	public  static String longestPalindrome(String s) {
    
    
		int max = 0;
		String va = "";
		if(s.length()<2)return s;//""和"a"
		int mid = (s.length()-1) / 2;//中间(偶数左侧,奇数中间)
		for (int i = 0; i < mid+1; i++) {
    
    
			
			int l = mid - i, r = l;//左奇数个
			String s1=getmaxhuiwen(l, r, s);
			va=va.length()>s1.length()?va:s1;
			l=mid-i;r=l+1;//左偶数个
			s1=getmaxhuiwen(l, r, s);
			va=va.length()>s1.length()?va:s1;
			l=mid+i;r=l;//右奇数个
			s1=getmaxhuiwen(l, r, s);
			va=va.length()>s1.length()?va:s1;
			l=mid+i;r=l+1;//右偶数个
			s1=getmaxhuiwen(l, r, s);
			va=va.length()>s1.length()?va:s1;
			max=va.length();//最大回文长度
			if(max>(mid-i+1)*2)//找不到更长直接返回
			{
    
    
				break;
			}
			
		}
		return va;
	}

In this case, the efficiency is good:
Insert picture description here

Conclusion

As for the problem solution area, some people have proposed a method of dynamic programming, but dynamic programming does not improve the efficiency of this problem very well. It will not be introduced here.

There is also a horse-drawn cart algorithm for asking for palindrome strings. I will write a special record of learning and understanding later, so stay tuned!

Finally, if it feels okay, remember to like, follow, and collect three consecutive times. The author's WeChat public account: bigsaiReply to the group to join the check-in activity, check-in for the second week, come on!

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40693171/article/details/107963480