. [Swift] LeetCode1177 palindromic sequence detector constructed | Can Make Palindrome from Substring

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤ micro-channel public number: to dare (WeiGanTechnologies)
➤ blog Park address: San-ching Wing Chi ( https://www.cnblogs.com/strengthen/ )
➤GitHub address: https://github.com/strengthen/LeetCode
➤ original address: HTTPS: //www.cnblogs. com / strengthen / p / 11443477.html 
➤ If the address is not a link blog Park Yong Shan Chi, it may be crawling author of the article.
➤ text has been modified update! Click strongly recommended that the original address read! Support authors! Support the original!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

Given a string s, we make queries on substrings of s.

For each query queries[i] = [left, right, k], we may rearrange the substring s[left], ..., s[right], and then choose up to k of them to replace with any lowercase English letter. 

If the substring is possible to be a palindrome string after the operations above, the result of the query is true. Otherwise, the result is false.

Return an array answer[], where answer[i] is the result of the i-th query queries[i].

Note that: Each letter is counted individually for replacement so if for example s[left..right] = "aaa", and k = 2, we can only replace two of the letters.  (Also, note that the initial string s is never modified by any query.)

 

Example :

Input: s = "abcda", queries = [[3,3,0],[1,2,0],[0,3,1],[0,3,2],[0,4,1]]
Output: [true,false,false,true,true]
Explanation:
queries[0] : substring = "d", is palidrome.
queries[1] : substring = "bc", is not palidrome.
queries[2] : substring = "abcd", is not palidrome after replacing only 1 character.
queries[3] : substring = "abcd", could be changed to "abba" which is palidrome. Also this can be changed to "baab" first rearrange it "bacd" then replace "cd" with "ab".
queries[4] : substring = "abcda", could be changed to "abcba" which is palidrome.

 

Constraints:

  • 1 <= s.length, queries.length <= 10^5
  • 0 <= queries[i][0] <= queries[i][1] < s.length
  • 0 <= queries[i][2] <= s.length
  • s only contains lowercase English letters.

 

Give you a string  s, you please  s sub string for testing.

Each test, to be detected can be expressed as a substring  queries[i] = [left, right, k]. We can rearrange the substring  s[left], ..., s[right], and select the most  k entries replace any lowercase letters. 

If the detection process, may become palindromic substring form character string, then the detection result is  true, otherwise the result is  false.

Return key array  answer[], which  answer[i] is the first  i one to be detected substring  queries[i] detection result.

Note: When you replace, substring Each letter must be counted as a separate item, that is, if  s[left..right] = "aaa" and  k = 2, we can only replace one of the two letters. (Further, any detected will not modify the original string  s, each detector may be considered are independent)

 

Example:

Input: s = "abcda", queries = [[3,3,0], [1,2,0], [0,3,1], [0,3,2], [0,4,1] ] 
output: [true, false, false, true, true] 
interpretation: 
Queries [0]: substring = "d", palindrome. 
queries [1]: substring = "bc", is not a palindrome. 
queries [2]: substring = "abcd", only the replacement of a character string is changed not a palindrome. 
queries [3]: substring = "abcd", can become palindromic "abba". Can also become "baab", to reorder become "bacd", then "cd" is replaced with "ab". 
queries [4]: substring = "abcda", can become palindromic "abcba".

 

prompt:

  • 1 <= s.length, queries.length <= 10^5
  • 0 <= queries[i][0] <= queries[i][1] < s.length
  • 0 <= queries[i][2] <= s.length
  • s Only lowercase letters

Guess you like

Origin www.cnblogs.com/strengthen/p/11443477.html