zoukankan      html  css  js  c++  java
  • Palindrome Permutation

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

    For example,
    "code" -> False, "aab" -> True, "carerac" -> True.

    解法一:

    解题思路

    The idea is to iterate over string, adding current character to set if set doesn't contain that character, or removing current character from set if set contains it. When the iteration is finished, just return set.size()==0 || set.size()==1.

    set.size()==0 corresponds to the situation when there are even number of any character in the string, and set.size()==1 corresponsds to the fact that there are even number of any character except one.

     1 public class Solution {
     2     public boolean canPermutePalindrome(String s) {
     3         Set<Character> set=new HashSet<Character>();
     4         for(int i=0; i<s.length(); ++i){
     5             if (!set.contains(s.charAt(i)))
     6                 set.add(s.charAt(i));
     7             else 
     8                 set.remove(s.charAt(i));
     9         }
    10         return set.size()==0 || set.size()==1;
    11     }
    12 }

    reference: https://leetcode.com/discuss/53295/java-solution-w-set-one-pass-without-counters

  • 相关阅读:
    hadoop中map和reduce的数量设置问题
    Flink基础
    Leetcode中的SQL题目练习(二)
    Redis-2- 基本概念
    Redis-1-安装
    C#事件(1)
    java(13)内部类
    java(12)异常处理
    java(11)构造器
    java(10)接口抽象类
  • 原文地址:https://www.cnblogs.com/hygeia/p/5062925.html
Copyright © 2011-2022 走看看