首页 > 试题广场 >

判断字符是否唯一

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

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

输入

"nowcoder"

输出

false

说明

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

输入

"nowcOder"

输出

true

说明

每个字符都只出现了一次 
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) {
    // 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)
//利用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)
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) {
     char [] chars=str.toCharArray();
     boolean a=true;
     for (int i=0;i<chars.length;i++){
       for (int j=i+1;j<chars.length;j++){
         if (chars[j]==chars[i]){
           a=false;
          }
       }
     }
     return a;
   }
}

发表于 2022-01-19 00:34:15 回复(0)
import java.util.*;

public class Solution {
    public boolean isUnique (String str) {
        long d =  Arrays.stream(str.split("")).distinct().count();
        return str.length() == d;
    }
}
发表于 2022-01-12 23:07:38 回复(0)
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)

问题信息

难度:
19条回答 4340浏览

热门推荐

通过挑战的用户

查看代码