首页 > 试题广场 >

确定字符互异

[编程题]确定字符互异
  • 热度指数:95597 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

给定一个字符串string iniString,请返回一个bool值,True代表字符串的所有字符全都不同,False代表存在相同的字符。保证字符串中的字符为ASCII字符且不允许使用额外的存储结构,字符串的长度小于等于3000。大小写字母算不同的字符

测试样例:
"aeiou"
返回:True
"BarackObama"
返回:False
推荐
public boolean checkDifferent(String iniString) {   
return !iniString.matches(".*(.)(.*\\1).*");
}

编辑于 2016-02-24 11:33:12 回复(87)
return len(iniString) == len(set(iniString))

发表于 2022-05-06 16:07:05 回复(0)
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        len_s=len(iniString)
        new_string=set(iniString)
        new_string=len(new_string)
        if new_string==len_s:
            return True
        else:
            return False
用字典解决长度
发表于 2021-03-14 20:13:03 回复(0)
'''思路:可以利用python里边的set()去重功能,如果set(字符串)的长度与原字符串长度一样,说明没有重复的元素,返回True,否则返回Flase.
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        if len(set(iniString))==len(iniString):
            return True
        else:
            return False
发表于 2020-10-06 22:24:14 回复(0)
class Different:
    def checkDifferent(self, iniString):
        import re
        res = re.findall(r'(.).*\1',iniString)
        return len(res)==0
正则爱好者
发表于 2020-02-10 11:46:49 回复(0)
# python判断字符是否重复两种方法 def is_unique(str1):
    char_list = []  for i in str1:  if i in char_list: print(False) return False  else:
            char_list.append(i)  print(str1)  return True  def is_unique2(str2):  for i in str2:  pass  if str2.count(i)>1:  print(False) else:  print(True)

发表于 2019-10-21 20:21:38 回复(0)
return len(set(iniString)) == len(iniString)
人生苦短
好像算用了额外的存储结构
编辑于 2018-10-17 21:43:01 回复(0)
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        return len(set(iniString))==len(iniString)
        # write code here

发表于 2018-06-01 16:46:48 回复(0)
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        return len(iniString) == len(set(iniString))
Python 一行式
编辑于 2018-01-26 11:00:36 回复(1)

python solution:

# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        return len(iniString)==len(set(iniString))
发表于 2017-10-01 20:07:54 回复(5)
一行代码搞定
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        return True if len(iniString)==len(set(list(iniString))) else False

发表于 2017-08-18 17:03:02 回复(0)
def quick_check(s):
  length = len(s)
  if len(s) <= 1:
    return True
  right = []
  left = []
  privot = s[0]
  flag = 0
  for i in range(1,len(s)):
    if s[i] > privot:
      right.append(s[i])
    else:
      left.append(s[i])
    if s[i] == privot:
      flag = 1

  if flag == 1:
    return False

  return quick_check(left) and quick_check(right)
发表于 2017-07-18 16:03:19 回复(0)
这题目简直神奇了,简便方法放机器上不论如何都能成功,这里死活输出是反的。好,说用了额外的存储结构--列表。我不用行了把,我暴力解决,**输出结果还反的:
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        for x in iniString:
            if ord(x) > 128 or len(iniString) > 3000:
                return 'Error'
        for i in range(len(iniString) - 1):
            for j in range(i+1, len(iniString)):
                if iniString[i] == iniString[j]:
                    return False
        else:
            return True
               
           
                   
       
发表于 2017-06-30 14:36:38 回复(0)
python思路:
1. 对字符串中的每一个字母计数,出现大于1就返回False。
2. set()去重,比较长度是否变化。
3. 依次取字母,看该字母是否在切片后(切掉包括该字母之前的所有字母)的字符串中
发表于 2017-06-05 11:14:03 回复(1)
class Different:
    def checkDifferent(self, iniString):
        # write code here
        from collections import Counter
        a=Counter(iniString).most_common(1)
        if a[0][1]>1:
            return False
        else:
            return True
通过计数器取得字符串中每个字符的次数,并从中取得出现次数最大的元素及其次数。如果最大次数大于一就返回False
发表于 2017-04-12 16:28:17 回复(0)
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        if len(iniString) == len(set(iniString)):
            return True
        else:
            return False

使用set先去掉重复字符,然后比较长度是否等于原符长度。
相等则没有重复字符
发表于 2017-04-11 19:01:50 回复(0)
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        for i in iniString:
            if iniString.count(i)>1:
                return False
        return True  
发表于 2017-04-07 09:50:28 回复(0)
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        return len(set(iniString)) == len(iniString)

发表于 2016-12-25 21:57:23 回复(0)
# -*- coding:utf-8 -*-
class Different:
    def checkDifferent(self, iniString):
        # write code here
        if len(iniString) == len(set(iniString)):
            return True
        else:
            return False
python就这样写就可以了额,set会去重,如果有重复的长度就不一样了,否则,就是没重复的哈
发表于 2016-12-01 21:21:10 回复(3)
return len(set(iniString)) == len(iniString)

发表于 2016-09-29 15:09:09 回复(0)