首页 > 试题广场 >

判断字符是否唯一

[编程题]判断字符是否唯一
  • 热度指数:3562 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定一个字符串,请你判断其中每个字符是否全都不同。

数据范围:字符串长度满足
示例1

输入

"nowcoder"

输出

false

说明

 "nowcoder" 中 'o' 出现了两次,因此返回 false 
示例2

输入

"nowcOder"

输出

true

说明

每个字符都只出现了一次 
import java.util.*;


public class Solution {
    Map<Character,Character> map=new HashMap<>();
    public boolean isUnique (String str) {
        for(int i=0;i<str.length();i++){
            if(map.get(str.charAt(i))!=null){
                return false;
            }else{
                map.put(str.charAt(i),'a');
            }
        }
        return true;
    }
}

发表于 2021-11-26 19:53:47 回复(0)
public boolean isUnique (String str) {
        long d =  Arrays.stream(str.split("")).distinct().count();
        return str.length() == d;
    }
发表于 2022-04-06 11:43:42 回复(0)
两种位运算的思路,注意处理好溢出
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return bool布尔型
     */
    public boolean isUnique (String str) {
        // write code here
        long last = 0;
        for(int i = 0; i < str.length(); i++) {
            long count = last ^  (1L << (str.charAt(i) - 'a'));
            
            if(count < last) return false;
            last = count;
            
        }
        return true;
        // long count = 0;
        // for (int i = 0; i < str.length(); i++) {
        //     int shift = str.charAt(i) - 'a'; // 假设字符串只包含小写字母
        //     if ((count & (1L << shift)) != 0) return false; // 检查对应位是否已经置为1
        //     count |= (1L << shift); // 设置该位
        // }
        // return true;
    }
}

编辑于 2024-01-14 00:09:00 回复(0)
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return bool布尔型
     */
    public boolean isUnique (String str) {
        // write code here
        if(str.length()==0){
            return true;
        }
        ArrayList<Character> list1=new ArrayList<>();
        for(int i=0;i<str.length();i++){
            if(!list1.contains(str.charAt(i))){
                list1.add(str.charAt(i));
            }else{
                return false;
            }
        }
        return true;
    }
}

发表于 2023-05-12 09:13:45 回复(0)
public boolean isUnique (String str) {
    List<Character> list = new ArrayList<>();
    for(int i = 0; i < str.length(); i++) {
        if (list.contains(str.charAt(i))) {
            return false;
        } else {
            list.add(str.charAt(i));
        }
    }
    return true;
}

发表于 2023-04-19 11:06:41 回复(0)
package main
import _"fmt"

/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param str string字符串 
 * @return bool布尔型
*/
func isUnique( str string ) bool {
    cnt:=map[byte]int{}
    for _,ch:=range []byte(str){
        cnt[ch]++
        if cnt[ch]>1{
            return false
        }
    }
    return true
}

发表于 2023-03-06 21:14:35 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param str string字符串 
 * @return bool布尔型
 */
int cmp(const void* e1,const void* e2){
    return *((char*)e1)-*((char*)e2);
}
bool isUnique(char* str ) {
    // write code here
    qsort(str,strlen(str),1,cmp);
    for(int i=1;i<strlen(str);i++){
        if(str[i]==str[i-1])
          return false;
    }
    return true;
}

发表于 2022-12-23 22:43:40 回复(0)
/**
 * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
 *
 * 
 * @param str string字符串 
 * @return bool布尔型
 */
bool isUnique(charstr ) {
    // write code here
    int a[128]={0},i;
    for(i=0;*(str+i)!='\0';i++)
    {
        int b=*(str+i);
        a[b]++;
        if(a[b]>1)
        {
            return false;
        }
    }
    return true;
}
发表于 2022-11-20 12:21:39 回复(0)
public boolean isUnique (String str) {
    // write code here
    for(int i = 0;i<str.length();i++){
        if(str.indexOf(str.charAt(i))!=str.lastIndexOf(str.charAt(i))){
            return false;
        }
    }
    return true;
}


发表于 2022-08-27 19:04:57 回复(0)
public boolean isUnique (String str) {
        // write code here
        int len = str.length();
        HashSet set = new HashSet(len);
        for (int i = 0; i < len; i++) {
            set.add(str.charAt(i));
        }
        if (len == set.size()) {
            return true;
        }else {
            return false;
        }
    }

发表于 2022-07-06 16:33:56 回复(0)
class Solution:
    def isUnique(self , str: str) -> bool:
        # write code here
        a= set(list(str))
        if len(a) == len(str):
            return True
        return False

发表于 2022-06-21 17:23:22 回复(0)
//利用set的特性,不能添加重复元素
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return bool布尔型
     */
    public boolean isUnique (String str) {
        // write code here
        Set<Character>set=new HashSet<>();
        char[]array=str.toCharArray();
        for(int i=0;i<array.length;i++){
            if(!set.add(array[i])){
                return false;
            }
            set.add(array[i]);
        }
        return true;
    }
}
发表于 2022-05-07 20:48:21 回复(0)
class Solution:
    def isUnique(self , str: str) -> bool:
        # write code here
        d = {}
        for ch in str:
            if ch in d:
                return False
            else:
                d[ch] = 1
        return True

发表于 2022-04-22 11:08:25 回复(0)
bool isUnique(char* str ) {
    // write code here
    int hash[128] = {0};
    int len = strlen(str);
    char *p = str;
    int num = 0;
    for(int i=0; i<len; i++)
    {
        num = hash[*(p+i)];
        if(num == 0)
            hash[*(p+i)]++;
        else
            return false;
    }
    return true;
}
发表于 2022-04-13 11:30:52 回复(0)
#coding:utf-8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#

# @param str string字符串 
# @return bool布尔型
#
class Solution:
    def isUnique(self , str ):
        # write code here
        listC={}
        for i in range(len(str)):
            listC[str[i]]=0
            for j in str:
                if str[i]==j:
                    listC[str[i]]+=1
        if max(listC.values())>1:
            return False
        return True
发表于 2022-04-02 01:30:18 回复(0)
class Solution:
    def isUnique(self , str ):
        # write code here
        
        for i in str:
            if (str.count(i)>1):
                return False
        return True
发表于 2022-03-16 22:21:45 回复(0)
位运算,大小写各开辟个数字,如果出现就将对应的那一位变为1,如果已经是1则说明重复出现;
class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return bool布尔型
     */
    bool isUnique(string str) {
        // write code here
        int big = 0, small = 0;
        for(int i = 0; i < str.size(); ++i){
            int mv = 1;
            if(str[i] <= 'Z')
            {
                int n = str[i] - 'A';
                while(n--)
                    mv = mv << 1;
                if((big & (mv)) != 0)
                    return false;
                big |= mv;
            }else{
                int n = str[i] - 'a';
                while(n--)
                    mv = mv << 1;
                if((small & (mv)) != 0)
                    return false;
                small |= mv;
            }
        }
        return true;
    }
};


发表于 2022-03-09 16:11:42 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param str string字符串 
# @return bool布尔型
#
class Solution:
    def isUnique(self , str: str) -> bool:
        # write code here
        return len(set(str)) == len(str)

发表于 2022-03-02 22:47:40 回复(0)
 *
 * C语言声明定义全局变量请加上static,防止重复定义
 */
int compar(const void *a,const void*b){
    
    return *(char *)a-*(char*)b;
}


bool isUnique(char* str ) {
    // write code here
    /*char a =[10000]={0};
         a=str;*/
        
     
   int l = strlen(str);
    int i;
    if(l<=1)return true;
    qsort(str,l,sizeof(char),compar);
    for(i=1;i<l;i++){
        
      if(str[i]==str[i-1])
       return false;
        
        
        
    }
        
    return true;
        
     
    
发表于 2022-02-26 23:28:05 回复(0)
    bool isUnique(string str) {
        int s[256]={0}; //桶
        for(int i=0;i<str.length();i++)
            if(!s[(int)str[i]])s[(int)str[i]]++;
            else return 0;
        return 1;
    }

发表于 2022-02-20 20:25:54 回复(0)

问题信息

难度:
25条回答 4268浏览

热门推荐

通过挑战的用户

查看代码