题解 | #查找兄弟单词#

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

import java.util.*;

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); String[] data = new String[n]; for (int i = 0; i < n; i++) { data[i] = sc.next(); } int count = 0; String target = sc.next(); int index = sc.nextInt(); List list = new ArrayList<>(); char[] targetArr = target.toCharArray(); Arrays.sort(targetArr); String sortTarget = new String(targetArr); for (String word : data) { if (word.length() != target.length()) { continue; }

            if (word.equals(target)) {
                continue;
            }
            char[] wordArr = word.toCharArray();
            Arrays.sort(wordArr);
            String sortWord = new String(wordArr);
            if (sortWord.equals(sortTarget)) {
                count++;
                list.add(word);
            }
        }
        System.out.println(count);
        int size = list.size();
        if (size > 0 && size > index - 1) {
            Collections.sort(list);
            System.out.println(list.get(index - 1));
        }
    }
}

}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务