【36】Segmentation of palindrome | valid letter dysphorisms (LC 131 | 242)

Split palindrome

Problem Description

Given a string s, split s into some substrings so that each substring is a palindrome.

Return all possible splitting schemes for s.

Example:
Insert picture description here

answer

Backtracking

Backtracking method: an exhaustive method of optimization achieved by depth-first search.

template:

Recursive backtracking:

void backtrack (参数){
    
    
	if (终止条件) output();
	else{
    
    
		for (int i=f(n,t);i<=g(n,t);i++) {
    
    
			处理节点;
			if (constraint()&&bound()) backtrack();//如果满足约束条件则递归
			回溯,撤销对节点的处理;
		}
	}
}

Iterative backtracking:

void iterativeBacktrack (){
    
    
	int t=1;//t为层数
	while (t>0) {
    
    
		if (f(n,t)<=g(n,t))
			for (int i=f(n,t);i<=g(n,t);i++) {
    
    
				x[t]=h(i);
				if (constraint(t)&&bound(t)) {
    
    
					if (solution(t)) output(x);
					else t++;
				}
			}
		else t--;
	}
}

The code of this question:

public class Solution {
    
    

    public List<List<String>> partition(String s) {
    
    
        int len = s.length();
        List<List<String>> res = new ArrayList<>();
        if (len == 0) {
    
    
            return res;
        }
        Deque<String> path = new ArrayDeque<>();
        backtracking(s, 0, len, path, res);
        return res;
    }

    public void backtracking(String s, int start, int len, Deque<String> path, List<List<String>> res) {
    
    
        if (start == len) {
    
    //终止条件
            res.add(new ArrayList<>(path));
            return;
        }else{
    
    
            for (int i = start; i < len; i++) {
    
    
                if (isPalindrome(s, start, i)) {
    
    //如果是回文串则递归
                    path.addLast(s.substring(start, i + 1));
                    backtracking(s, i + 1, len, path, res);
                    path.removeLast();
                }
            }
        }
    }
    
    public boolean isPalindrome(String s, int low, int high) {
    
    
        while (low < high) {
    
    
            if (s.charAt(low) != s.charAt(high)) {
    
    
                return false;
            }
            low++;
            high--;
        }
        return true;
    }
}

Valid letter variants

Problem Description

Given two strings s and t, write a function to determine whether t is an anagram of s.

Description:

  • Aliphatic words refer to strings with the same letters but different arrangements.
  • You can assume that the string contains only lowercase letters.

Advanced

  • What if the input string contains unicode characters? Can you adjust your solution to deal with this situation?

answer

hash table

Drawing solution algorithm: 242. Effective letter dysphorisms

thought:

  • First judge whether the two strings are equal in length, and return false directly if they are not equal
  • If they are equal, initialize the 26-letter hash table and traverse the strings s and t
  • s is responsible for increasing in the corresponding position, t is responsible for decreasing in the corresponding position
  • If the value of the hash table is 0, then the two are letter dysphoric words
class Solution {
    
    
    public boolean isAnagram(String s, String t) {
    
    
        if(s.length() != t.length())
            return false;
        int[] alpha = new int[26];
        for(int i = 0; i< s.length(); i++) {
    
    
            alpha[s.charAt(i) - 'a'] ++;
            alpha[t.charAt(i) - 'a'] --;
        }
        for(int i=0;i<26;i++)
            if(alpha[i] != 0)
                return false;
        return true;
    }
}

作者:guanpengchn
链接:https://leetcode-cn.com/problems/valid-anagram/solution/hua-jie-suan-fa-242-you-xiao-de-zi-mu-yi-wei-ci-by/
来源:力扣(LeetCode)

Sort

This solution comes from the official solution

Thought: t is an ectopic word of s equivalent to "two strings are equal after sorting." Therefore, we can sort the strings s and t separately and judge whether the sorted strings are equal. In addition, if the lengths of s and tt are different, t must not be an ectopic word of s.

class Solution {
    
    
    public boolean isAnagram(String s, String t) {
    
    
        if (s.length() != t.length()) {
    
    
            return false;
        }
        char[] str1 = s.toCharArray();
        char[] str2 = t.toCharArray();
        Arrays.sort(str1);
        Arrays.sort(str2);
        return Arrays.equals(str1, str2);
    }
}

Guess you like

Origin blog.csdn.net/qq_43424037/article/details/113774630