找出字符串中第一个只出现一次的字符
数据范围:输入的字符串长度满足
import java.util.*; import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws Exception { BufferedReader bu = new BufferedReader(new InputStreamReader(System.in) ); // 注意 hasNext 和 hasNextLine 的区别 String b = bu.readLine(); String[] arr = b.split(""); for(int i=0;i<arr.length;i++){ if(!b.substring(i+1).contains(arr[i]) && !b.substring(0,i).contains(arr[i])){ System.out.println(arr[i]); break; } if(i==arr.length-1){ System.out.println(-1); } } } }
import java.util.Scanner; import java.util.LinkedHashMap; import java.util.Set; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 用repeat来存储已经被判断为重复重现的字符,后续再遇到该字符直接跳过 String inputStr = in.nextLine(); StringBuilder repeat = new StringBuilder(); for (int i = 0; i < inputStr.length(); i++) { // 如果该字符没有被判断过重复出现 且 最后一次出现的位置和当前位置相同,说明只出现了一次 if (!repeat.toString().contains(Character.toString(inputStr.charAt(i))) && i == inputStr.lastIndexOf(inputStr.charAt(i))) { System.out.println(inputStr.charAt(i)); return; } else { repeat.append(inputStr.charAt(i)); } } System.out.println(-1); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); String res = null; for (int i = 0; i < str.length(); i++) { if (str.indexOf(str.charAt(i)) == str.lastIndexOf(str.charAt(i))) { res = String.valueOf(str.charAt(i)); break; } } System.out.println(res == null ? -1 : res); } }
import java.util.Scanner; import java.util.HashMap; import java.util.Map; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); Map<Character, Integer> map = new HashMap<>(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (map.get(c) == 1) { System.out.println(c); return; } } System.out.println(-1); } }
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String line = in.nextLine(); HashMap<Character, Integer> map = new HashMap<>(); // select the first character only appears once for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } for (int i = 0; i < line.length(); i++) { char c = line.charAt(i); if (map.get(c) == 1) { System.out.println(c); return; } } System.out.println("-1"); } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); String tmp = str; String res = null; while(tmp.length() != 0){ String tp = String.valueOf(tmp.charAt(0)); String st = tmp.replaceAll(tp, ""); if(tmp.length()-st.length()==1){ res = tp; break; }else{ tmp = st; } } if(res==null){ System.out.println("-1"); }else{ System.out.println(res); } } }
import java.util.Scanner; import java.util.ArrayList; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 if (in.hasNext()) { String inString = in.next(); for (int i = 0; i < inString.length(); i++) { char a = inString.charAt(i); if (inString.lastIndexOf(a) == inString.indexOf(a)) { System.out.println(a); return; } } System.out.println("-1"); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); char[] ch=sc.nextLine().toCharArray(); Map<Character,Integer> map=new LinkedHashMap<>(); for(int i=0;i<ch.length;i++){ if(!map.containsKey(ch[i])){ map.put(ch[i],1); }else{ map.put(ch[i],map.get(ch[i])+1); } } Set entrySet=map.entrySet(); Iterator it=entrySet.iterator(); int time=0; while(it.hasNext()){ Map.Entry entry=(Map.Entry)it.next(); if((Integer)entry.getValue()==1){ Character key=(Character)entry.getKey(); time++; System.out.println(key); break; } } if(time==0){ System.out.println(-1); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str=in.nextLine(); //去重后的集合 ArrayList<Character> list1=new ArrayList<>(); //重复的元素集合 ArrayList<Character> list2=new ArrayList<>(); for(int i=0;i<str.length();i++){ if(!list1.contains(str.charAt(i))){ list1.add(str.charAt(i)); }else{ list2.add(str.charAt(i)); } } for(int i=0;i<str.length();i++){ if(!list2.contains(str.charAt(i))){ System.out.print(str.charAt(i)); return; } } System.out.print(-1); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String a = in.next(); Map<Character, Integer> map = new LinkedHashMap<Character, Integer>(); for (int i = 0; i < a.length(); i++) { map.put(a.charAt(i), map.getOrDefault(a.charAt(i), 0) + 1); } StringBuffer sub2 = new StringBuffer(); map.forEach((key, val)-> { if (val == 1) { sub2.append(key); } }); if (sub2.length() > 0) { System.out.println(sub2.substring(0, 1)); } else { System.out.println(-1); } } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); Map<Character, Integer> map = new HashMap<>(); char[] chs = str.toCharArray(); for(char ch : chs){ int count = map.getOrDefault(ch,0)+1; map.put(ch,count); } for(char ch : chs){ if(map.get(ch) == 1){ System.out.println(ch); return; } } System.out.println(-1); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextLine()) { // 注意 while 处理多个 case String str = in.nextLine(); char[] arr=str.toCharArray(); int length=str.length(); for(int i=0;i<str.length();i++){ String str1=str.replace(String.valueOf(arr[i]),""); if(str1.length()+1==length){ System.out.println(arr[i]); break; }else if(str1.length()+2==length){ if(i==str.length()-1){ System.out.println(-1); } continue; } } } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); char d = ' '; for (char c : str.toCharArray()) { if (str.length() - str.replaceAll(c + "", "").length() == 1) { d = c; break; } } // 注意这里如果没有+ ""会输出d的ASCII码111 System.out.println(d == ' ' ? -1 : d + ""); } }