题解 | #牛群密码 - 有效回文#
牛群密码 - 有效回文
https://www.nowcoder.com/practice/98fad63b47544d5ebf4042fc53b54b3d?tpId=354&tqId=10588466&ru=/exam/oj/ta&qru=/ta/interview-202-top/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D354
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param password string字符串 * @param k int整型 * @return bool布尔型 */ public boolean isValidPalindrome (String password, int k) { // write code here Map<Character, Integer> map = new HashMap<>(); char[] chs = password.toCharArray(); for (char c : chs) { if (map.containsKey(c)) { map.put(c, map.get(c) + 1); } else { map.put(c, 1); } } if (map.keySet().size() > k) return false; for (char c : map.keySet()) { if (map.get(c) == 1) { if(huiwenshu(password.replace(String.valueOf(c),""))){ return true; }else return false; } } return false; } private static boolean huiwenshu(String s) { if (s == null || s.length() == 0) return true; for (int i = 0, j = s.length() - 1; i <= j; i++, j--) { if (s.charAt(i) != s.charAt(j)) { return false; } } return true; } }
面试高频TOP202 文章被收录于专栏
面试高频TOP202题解