java 判断一个字串中的字符全是字母

本文介绍了一个用于检查字符串中所有字符是否均为字母的简单方法,通过遍历字符串并验证每个字符是否属于字母范围来实现。
记录一个方法,用来判断一个字串中字符是否全为字母

public class MainClass {
    public static void main(String[] args){   
        
        String str = "hhhggdxszfff";  
        boolean is_boolean = isPhonticName(str);
        System.out.println(is_boolean);
}

    
    public static boolean isPhonticName(String str) {
        char[] chars=str.toCharArray();
        boolean isPhontic = false;
        for(int i = 0; i < chars.length; i++) {
            isPhontic = (chars[i] >= 'a' && chars[i] <= 'z') || (chars[i] >= 'A' && chars[i] <= 'Z');
            if (!isPhontic) {
                return false;
            }
        }
        return true;
    }

}


Java 中,统计字符串中最长子串可以根据不同的条件和需求采用不同的方法,以下为几种常见的情况及实现: ### 至少有 K 个重复字符的最长子串 可使用递归分治的方法,统计每个字母出现的次数,排除频次小于 k 的字符,再递归处理剩余子串。 ```java class Solution { public int longestSubstring(String s, int k) { int len = s.length(); if (len == 0 || k > len) { return 0; } if (k < 2) { return len; } return count(s.toCharArray(), k, 0, len - 1); } private static int count(char[] chars, int k, int left, int right) { if (right - left + 1 < k) return 0; int[] times = new int[26]; // 26个字母 for (int i = left; i <= right; ++i) { times[chars[i] - 'a']++; // 统计每个字母出现的次数,字符出现频次小于k,则不可能出现在结果子串中 } // 分别排除,然后挪动两个指针 while (right - left + 1 >= k && times[chars[left] - 'a'] < k) { ++left; } while (right - left + 1 >= k && times[chars[right] - 'a'] < k) { --right; } if (right - left + 1 < k) { // 排除到剩余的字符串小于k,则直接return return 0; } // 得到临时子串,再递归处理 for (int i = left; i <= right; ++i) { // 如果第i个不符合要求,切分成左右两段分别递归求得 if (times[chars[i] - 'a'] < k) { return Math.max(count(chars, k, left, i - 1), count(chars, k, i + 1, right)); } } return right - left + 1; } } ``` 使用示例: ```java public class Main { public static void main(String[] args) { Solution solution = new Solution(); String s = "ababbc"; int k = 2; System.out.println(solution.longestSubstring(s, k)); } } ``` ### 无重复字符的最长子串 可使用滑动窗口的思想,定义两个指针 `start` 和 `end`,利用 `HashSet` 存放符合条件的子串,遍历字符串并动态调整窗口。 ```java import java.util.HashSet; public class LongestSubstringWithoutRepeatingCharacters { public int lengthOfLongestSubstring(String s) { int start = 0; int end = 0; int maxLength = 0; HashSet<Character> set = new HashSet<>(); while (end < s.length()) { if (!set.contains(s.charAt(end))) { set.add(s.charAt(end)); end++; maxLength = Math.max(maxLength, set.size()); } else { set.remove(s.charAt(start)); start++; } } return maxLength; } public static void main(String[] args) { LongestSubstringWithoutRepeatingCharacters solution = new LongestSubstringWithoutRepeatingCharacters(); String s = "abcabcbb"; System.out.println(solution.lengthOfLongestSubstring(s)); } } ``` ### 长度大于等于 L 且出现次数最多的子串 可以枚举所有可能的子串,统计其出现次数,找出符合条件的子串。 ```java import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class SubstringStatistics { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int L = scanner.nextInt(); scanner.nextLine(); String S = scanner.nextLine(); String result = findMaxSubstring(S, L); System.out.println(result); } public static String findMaxSubstring(String S, int L) { Map<String, Integer> substringCount = new HashMap<>(); int n = S.length(); for (int i = 0; i < n; i++) { for (int j = i + L; j <= n; j++) { String sub = S.substring(i, j); substringCount.put(sub, substringCount.getOrDefault(sub, 0) + 1); } } String maxSubstring = ""; int maxCount = 0; for (Map.Entry<String, Integer> entry : substringCount.entrySet()) { String sub = entry.getKey(); int count = entry.getValue(); if (count > maxCount || (count == maxCount && sub.length() > maxSubstring.length()) || (count == maxCount && sub.length() == maxSubstring.length() && S.indexOf(sub) < S.indexOf(maxSubstring))) { maxCount = count; maxSubstring = sub; } } return maxSubstring; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值