【leetcode 2949 统计美丽子字符串】
import java.util.HashMap; import java.util.Map; class Solution { public static void main(String[] args) { Solution solution = new Solution(); long ans = solution.beautifulSubstrings("baeyh",2); System.out.println(ans); } public long beautifulSubstrings(String s, int k) { int k0 = pSqrt(4 * k); Map<String, Integer> map = new HashMap<>(); long ans = 0; int sum = 0; map.put((k0-1)+",0",1); for (int i = 0; i < s.length(); i++) { if (isVowels(s.charAt(i))) { sum += 1; } else { sum -= 1; } String key = (i%k0) +","+ sum; map.put(key,map.getOrDefault(key,0)+1); ans += map.get(key) - 1; } return ans; } public int pSqrt(int n) { int i = 2; int res = 1; while (i * i <= n) { int x = i * i; while (n % x == 0) { res *= i; n = n / x; } if (n % i == 0) { res *= i; n = n / i; } i++; } if (n != 1) { res = res * n; } return res; } // public boolean isVowels(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { return true; } return false; } }
本文作者:love
本文链接:https://www.cnblogs.com/fishcanfly/p/17870858.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步