这题可以直接用类似于滑动窗口的思想来做
参考代码:
import java.util.HashSet; import java.util.LinkedList; import java.util.Scanner; import java.util.Set; public class T2911 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); String str = sc.next(); int cnt = 0; int index = 0; LinkedList<Character> result = new LinkedList<>(); for (int i = 0; i < str.length(); i++) { result.add(str.charAt(i)); cnt++; while(!f(result)) { result.removeFirst(); cnt--; } if(cnt == n) { index = i; break; } } if(cnt != n) { System.out.println("No"); }else { System.out.println(result.getFirst()); } } public static boolean f(LinkedList<Character> result) { Set<Character> set = new HashSet<>(result); if(set.size() == 1) { return true; } return false; } }
0.0分
1 人评分