首页 > 试题广场 >

判断字符是否唯一

[编程题]判断字符是否唯一
  • 热度指数:4027 时间限制: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 {

    public boolean isUnique (String str) {
        // write code here
        int[] cnt = new int[128]; // ASCAII码
        for (char c : str.toCharArray()) {
            if (++cnt[c] > 1) return false;
        }
        return true;
    }
}

发表于 2025-04-03 19:53:38 回复(0)
class Solution:
def isUnique(self , str: str) -> bool:
# write code here
s=set(str)
if len(str)==len(s):
return True
return False
发表于 2024-12-27 15:20:35 回复(0)
class Solution:
    def isUnique(self , str: str) -> bool:
       
        for index,item in enumerate(str):
       
            if item in str[index+1:]:
                return False

        return True
发表于 2024-09-16 09:44:51 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param str string字符串 
# @return bool布尔型
#
class Solution:
    def isUnique(self , str: str) -> bool:
        str_s_l = list(set(str))
        
        if len(str_s_l) != len(str):
            return(False)
        else:
            return(True)

发表于 2024-07-18 09:21:25 回复(0)
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param str string字符串 
# @return bool布尔型
#
class Solution:
    def isUnique(self , str: str) -> bool:
        # write code here
        str_copy = str 
        l1 = len(str)
        str_set = set(str)
        l2 = len(str_set)
        if l1 == l2:
            return True
        else:
            return False

发表于 2024-05-18 16:16:57 回复(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)