确定一个字符串是否可以通过重新排列组合来形成回文字符串。

huangapple go评论81阅读模式
英文:

Given a string, determine if a permutation of the string could form a palindrome

问题

  1. 在类似的问题中,为什么会初始化一个大小为128或256的数组?有没有特殊的原因?

  2. arr[s.charAt(i)]++; 这个语法我不太明白,有人可以解释一下这里具体发生了什么吗?如果有人能给我提供一个了解这种语法的链接,那就太好了。

以下是翻译好的部分:

  1. 在类似的问题中,为什么会初始化一个大小为128或256的数组?有没有特殊的原因?

  2. arr[s.charAt(i)]++; 这个语法我不太明白,有人可以解释一下这里具体发生了什么吗?如果有人能给我提供一个了解这种语法的链接,那就太好了。

英文:

I am solving this problem on Leetcode and I have seen a solution, but I am having trouble understanding some syntax used in the solution. If anyone can explain me, I would really appreciate it.

Problem:
<https://leetcode.com/problems/palindrome-permutation/>

Solution:

class Solution {
    public boolean canPermutePalindrome(String s) {
        
        int count=0;
        int[]arr=new int[128];
        for(int i=0;i&lt;s.length();i++){
            arr[s.charAt(i)]++;
       
        if(arr[s.charAt(i)]%2==0)
            count--;
        
        else 
            count++;
    
        }
        return count &lt;=1;
    }
}

My questions:

  1. I have seen other similar problems initialize an array of size 128 or 256. Why do we use 128 or 256 for array size in problems like this? is there any particular reason?

  2. arr[s.charAt(i)]++; &lt;&lt; I don't understand this syntax, can anyone explain me what exactly is going in here? If anyone can refer me to a link for understanding syntax like this, it would be nice.

答案1

得分: 1

  1. 为什么在这种问题中我们要使用128或256作为数组大小?这用于字符实例计数。字符预计在0-127或0-255的范围内。

  2. arr[s.charAt(i)]++; << 我不理解这个语法,有人可以解释一下这里到底发生了什么吗?这会增加字符串(s.charAt(i))中每个字符的计数。

对于一个正确形成的回文排列,最多只能有一个具有奇数次实例的字符,因为唯一可以放置具有奇数次实例的字符的地方是回文的中点。

英文:
  1. Why do we use 128 or 256 for array size in problems like this? This is used for character instance counting. Characters are expected to be in the range of 0-127 or 0-255
  2. arr[s.charAt(i)]++; << i don't understand this syntax, can anyone explain me what exactly is going in here - this is incrementing the count for character each character in the string (s.charAt(i))

For a properly formed palindrome permutation, there can be no more than one character that has an odd number of instances, as the only place to put a character with an odd number of instances is at the midpoint of the palindrome.

答案2

得分: 0

  1. 数组长度基于字符集是ASCII还是扩展ASCII而取为128或256。请参考 http://www.asciitable.com/
  2. arr[s.charAt(i)]++ 表示字符递增。在Java中,char是数值类型。当你将1加到char上时,你会得到下一个Unicode码点。
    arr[s.charAt(i)]++ 翻译为:
    arr[s.charAt(i)] = arr[s.charAt(i)] + 1;
英文:
  1. The array length is taken as 128 or 256 on basis of whether the character set is ascii or extended ascii. please refer http://www.asciitable.com/
  2. arr[s.charAt(i)]++ means a character increment. In Java, char is a numeric type. When you add 1 to a char, you get to the next unicode code point.
    arr[s.charAt(i)]++ translates to :
    arr[s.charAt(i)] = arr[s.charAt(i)] +1;

答案3

得分: 0

尝试这个。

  • 它对字符进行频率计数。
  • 然后计算具有奇数频率的字符的数量。
  • 如果您有超过一个奇数次出现的字符集,它无法重新排列成回文。
public static boolean isConvertable(String a) {
    long count = Arrays.stream(a.split(""))
            .collect(Collectors.groupingBy(str -> str,
                    Collectors.counting()))
            .values().stream().filter(val -> (val % 2) == 1)
            .count();

    return count <= 1;
}
英文:

Try this.

  • it does a frequency count of the characters.
  • Then it counts the number of characters that have an odd frequency.
  • if you have more than one set of characters that occur an odd number of times it
    can't be rearranged into a palindrome.
public static boolean isConvertable(String a) {
	long count = Arrays.stream(a.split(&quot;&quot;))
			.collect(Collectors.groupingBy(str -&gt; str,
					Collectors.counting()))
			.values().stream().filter(val -&gt; (val % 2) == 1)
			.count();
	
    return count &lt;= 1;
}

</details>



huangapple
  • 本文由 发表于 2020年8月13日 00:38:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63381069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定