题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
比较两个String是不是兄弟字符串?
- 传成 char[];
- sort();
- Arrays.equal(ch1[], ch2[]);
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); String[] strs = str.split(" "); int num = Integer.parseInt(strs[0]); int knum = Integer.parseInt(strs[strs.length - 1]); String word = strs[strs.length - 2]; List<String> list = new ArrayList<>(); List<String> broWords = new ArrayList<>(); for (int i = 1; i < strs.length - 2; i++) { list.add(strs[i]); } char[] wordCh = word.toCharArray(); Arrays.sort(wordCh); for (String s : list) { char[] sCh = s.toCharArray(); Arrays.sort(sCh); if (!word.equals(s) && Arrays.equals(wordCh, sCh)) { broWords.add(s); } } Collections.sort(broWords); System.out.println(broWords.size()); if (broWords.size() > knum - 1) { System.out.println(broWords.get(knum - 1)); } } }