题解 | #查找兄弟单词#

查找兄弟单词

http://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.hasNextLine()) {
            String[] str = sc.nextLine().split("\\s+");
            int count = Integer.parseInt(str[0]);
            String test = str[str.length-2];
            int k = Integer.parseInt(str[str.length-1]);
            String[] original = new String[count];
            
            for(int i = 0; i < count; i++) {
                original[i] = str[i+1];
            }
            
            char[] chs = test.toCharArray();
            sort(chs);
          // Use this to sort this string, in case to judge if this element is equals another string
          
            String sss = new String(chs);
            
            List<String> s = new ArrayList<>();
            for(int i = 0; i < original.length; i++) {
                if(test.length() == original[i].length() && !test.equals(original[i])) {
                    char[] cs = original[i].toCharArray();
                    // Sort this array, compare to this check string
                    sort(cs);
                    if((new String(cs)).equals(sss)) {
                        s.add(original[i]);
                    }
                }
            }
                           
            System.out.println(s.size());
            if(k <= s.size() -1 ) {
                Collections.sort(s);
                System.out.println(s.get(k-1));
            }
            
        }
    }
  
  
  // Sort method, a little skill, Insertion sort function  
    private static void sort(char[] strs) {
        for(int i = 0; i < strs.length; i++) {
            for(int j = i; j > 0; j--) {
                if(strs[j] < strs[j-1]) {
                    char tmp = strs[j-1];
                    strs[j-1] = strs[j];
                    strs[j] = tmp;
                }
            }
         }
    }
    
}

// Thank you for your appreciation
全部评论

相关推荐

点赞 评论 收藏
分享
炫哥_:哥们项目描述里面vector和mysql之类的都要写吗,直接开头技术栈巴拉巴拉就行了,完全不是技术点啊
点赞 评论 收藏
分享
牛客83700679...:简历抄别人的,然后再投,有反馈就是简历不行,没反馈就是学历不行,多投多改只要技术不差机会总会有的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务