一个字符串的 美丽值 定义为:出现频率最高字符与出现频率最低字符的出现次数之差。
比方说,"abaacc"
的美丽值为 3 - 1 = 2
。
给你一个字符串 s
,请你返回它所有子字符串的 美丽值 之和。
示例 1:
**输入:** s = "aabcb"
**输出:** 5
**解释:** 美丽值不为零的字符串包括 ["aab","aabc","aabcb","abcb","bcb"] ,每一个字符串的美丽值都为 1 。
示例 2:
**输入:** s = "aabcbaa"
**输出:** 17
提示:
1 <= s.length <= 500
s
只包含小写英文字母。
方法一:双层循环 思路
用两个下标 i 和 j 表示子字符串的两端。用双层循环来遍历所有子字符串,第一层循环子字符串的起点 i,第二层循环固定 i,遍历子字符串的重点 j,遍历时维护更新用来记录字符频率的哈希表,并计算美丽值。
代码
[sol1-Python3] 1 2 3 4 5 6 7 8 9 10 11 class Solution : def beautySum (self, s: str ) -> int : res = 0 for i in range (len (s)): cnt = Counter() mx = 0 for j in range (i, len (s)): cnt[s[j]] += 1 mx = max (mx, cnt[s[j]]) res += mx - min (cnt.values()) return res
[sol1-C++] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Solution {public : int beautySum (string s) { int res = 0 ; for (int i = 0 ; i < s.size (); i++) { vector<int > cnt (26 ) ; int maxFreq = 0 ; for (int j = i; j < s.size (); j++) { cnt[s[j] - 'a' ]++; maxFreq = max (maxFreq, cnt[s[j] - 'a' ]); int minFreq = s.size (); for (int k = 0 ; k < 26 ; k++) { if (cnt[k] > 0 ) { minFreq = min (minFreq, cnt[k]); } } res += maxFreq - minFreq; } } return res; } };
[sol1-Java] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 class Solution { public int beautySum (String s) { int res = 0 ; for (int i = 0 ; i < s.length(); i++) { int [] cnt = new int [26 ]; int maxFreq = 0 ; for (int j = i; j < s.length(); j++) { cnt[s.charAt(j) - 'a' ]++; maxFreq = Math.max(maxFreq, cnt[s.charAt(j) - 'a' ]); int minFreq = s.length(); for (int k = 0 ; k < 26 ; k++) { if (cnt[k] > 0 ) { minFreq = Math.min(minFreq, cnt[k]); } } res += maxFreq - minFreq; } } return res; } }
[sol1-C#] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class Solution { public int BeautySum (string s ) { int res = 0 ; for (int i = 0 ; i < s.Length; i++) { int [] cnt = new int [26 ]; int maxFreq = 0 ; for (int j = i; j < s.Length; j++) { cnt[s[j] - 'a' ]++; maxFreq = Math.Max(maxFreq, cnt[s[j] - 'a' ]); int minFreq = s.Length; for (int k = 0 ; k < 26 ; k++) { if (cnt[k] > 0 ) { minFreq = Math.Min(minFreq, cnt[k]); } } res += maxFreq - minFreq; } } return res; } }
[sol1-C] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) int beautySum (char * s) { int res = 0 , len = strlen (s); for (int i = 0 ; i < len; i++) { int cnt[26 ]; memset (cnt, 0 , sizeof (cnt)); int maxFreq = 0 ; for (int j = i; j < len; j++) { cnt[s[j] - 'a' ]++; maxFreq = MAX(maxFreq, cnt[s[j] - 'a' ]); int minFreq = len; for (int k = 0 ; k < 26 ; k++) { if (cnt[k] > 0 ) { minFreq = MIN(minFreq, cnt[k]); } } res += maxFreq - minFreq; } } return res; }
[sol1-JavaScript] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var beautySum = function (s ) { let res = 0 ; for (let i = 0 ; i < s.length ; i++) { const cnt = new Array (26 ).fill (0 ); let maxFreq = 0 ; for (let j = i; j < s.length ; j++) { cnt[s[j].charCodeAt () - 'a' .charCodeAt ()]++; maxFreq = Math .max (maxFreq, cnt[s[j].charCodeAt () - 'a' .charCodeAt ()]); let minFreq = s.length ; for (let k = 0 ; k < 26 ; k++) { if (cnt[k] > 0 ) { minFreq = Math .min (minFreq, cnt[k]); } } res += maxFreq - minFreq; } } return res; };
[sol1-Golang] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 func beautySum (s string ) (ans int ) { for i := range s { cnt := [26 ]int {} maxFreq := 0 for _, c := range s[i:] { cnt[c-'a' ]++ maxFreq = max(maxFreq, cnt[c-'a' ]) minFreq := len (s) for _, c := range cnt { if c > 0 { minFreq = min(minFreq, c) } } ans += maxFreq - minFreq } } return } func min (a, b int ) int { if a < b { return a } return b } func max (a, b int ) int { if a > b { return a } return b }
复杂度分析