首页 > 其他分享 >leetcode-2423-easy

leetcode-2423-easy

时间:2022-11-01 22:23:59浏览次数:58  
标签:arr set word letter int 2423 frequency easy leetcode

Remove Letter To Equalize Frequency

You are given a 0-indexed string word, consisting of lowercase English letters. You need to select one index and remove the letter at that index from word so that the frequency of every letter present in word is equal.

Return true if it is possible to remove one letter so that the frequency of all letters in word are equal, and false otherwise.

Note:

The frequency of a letter x is the number of times it occurs in the string.
You must remove exactly one letter and cannot chose to do nothing.
 

Example 1:

Input: word = "abcc"
Output: true
Explanation: Select index 3 and delete it: word becomes "abc" and each character has a frequency of 1.
Example 2:

Input: word = "aazz"
Output: false
Explanation: We must delete a character, so either the frequency of "a" is 1 and the frequency of "z" is 2, or vice versa. It is impossible to make all present letters have equal frequency.
 

Constraints:

2 <= word.length <= 100
word consists of lowercase English letters only.

思路一: 用 26 长度的数组存储字符出现的次数,然后双层循环模拟减一的情况。也可以不用循环的方式判断,但是需要考虑的情况有点多,难怪通过率如此之低。为了代码清晰度,感觉 N 平方的复杂度是可以接受的

public boolean equalFrequency(String word) {
	int[] arr = new int[26];

	for (int i = 0; i < word.length(); i++) {
		arr[word.charAt(i) - 'a']++;
	}

	for (int i = 0; i < arr.length; i++) {
		if (arr[i] == 0) continue;
		int temp = arr[i];
		arr[i] = arr[i] - 1;
		Set<Integer> set = new HashSet<>();
		for (int j = 0; j < arr.length; j++) {
			if (arr[j] != 0) {
				set.add(arr[j]);
			}

			if (set.size() > 1) break;
		}
		if (set.size() == 1) return true;
		arr[i] = temp;
	}

	return false;
}

标签:arr,set,word,letter,int,2423,frequency,easy,leetcode
From: https://www.cnblogs.com/iyiluo/p/16849367.html

相关文章

  • easyExcel追加内容
        Filefile=newFile("test.xlsx");    FiletempFile=newFile("temp.xlsx");    Wordword=newWord();    word.w="AAAkkk22......
  • leetcode-2144-easy
    MinimumCostofBuyingCandiesWithDiscountAshopissellingcandiesatadiscount.Foreverytwocandiessold,theshopgivesathirdcandyforfree.Thec......
  • leetcode-1460-easy
    MakeTwoArraysEqualbyReversingSubarraysYouaregiventwointegerarraysofequallengthtargetandarr.Inonestep,youcanselectanynon-emptysubarra......
  • leetcode-257-easy
    BinaryTreePathsGiventherootofabinarytree,returnallroot-to-leafpathsinanyorder.Aleafisanodewithnochildren.Example1:Input:root=......
  • leetcode-1304-easy
    FindNUniqueIntegersSumuptoZeroGivenanintegern,returnanyarraycontainingnuniqueintegerssuchthattheyaddupto0.Example1:Input:n=5O......
  • leetcode-118-easy
    Pascal'sTriangleGivenanintegernumRows,returnthefirstnumRowsofPascal'striangle.InPascal'striangle,eachnumberisthesumofthetwonumbersdir......
  • leetcode-1137-easy
    N-thTribonacciNumberTheTribonaccisequenceTnisdefinedasfollows:T0=0,T1=1,T2=1,andTn+3=Tn+Tn+1+Tn+2forn>=0.Givenn,returnthe......
  • python: easyocr的安装和使用(easyocr 1.6.2 / Python 3.7.15 )
    一,安装easyocr:1,官网:https://www.jaided.ai/项目代码地址:https://github.com/JaidedAI/EasyOCR通过pip安装:[root@blog~]#pip3installeasyocr查看......
  • 如何在EasyCVR平台配置AI智能识别的微信端告警消息推送?
    我们在此前的文章中和大家分享过关于EasyCVR视频融合平台智能告警相关的开发及功能介绍,其中包括微信端的开发流程分享,感兴趣的用户可以翻阅往期的文章进行了解。智能告警功......
  • EasyCVR平台基于萤石云SDK接入的设备播放流程及接口调用
    EasyCVR视频融合云服务支持海量视频汇聚与管理、处理与分发、智能分析等视频能力,在功能上,可支持视频监控直播、云端录像、云存储、录像检索与回看、智能告警、平台级联、服......