给定一个字符串,请你判断其中每个字符是否全都不同。
数据范围:字符串长度满足 
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; } }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @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)
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @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
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; } }
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; } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @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; }
class Solution: def isUnique(self , str: str) -> bool: # write code here a= set(list(str)) if len(a) == len(str): return True return False