11.6 赎金信(哈希表)——【LeetCode】

11.6 赎金信(哈希表)——【LeetCode】,第1张

11.6 赎金信(哈希表)——【LeetCode】

package com.haxitable.java;

public class six {
	
	public static void main(String[] args) {
		String str = "a";
		str.charAt(0);
	}
	

	 public boolean canConstruct(String ransomNote, String magazine) {
		//记录magazine字符串出现的次数
		 int[] arr = new int[26];
		 int temp;
		 for(int i = 0; i < magazine.length(); i++) {
			 temp = magazine.charAt(i) - 'a';//用数组索引代替26个字母 值为出现的次数
			 arr[temp]++;			 
		 }
		 for(int i = 0; i < ransomNote.length(); i++) {
			 temp = ransomNote.charAt(i) - 'a';//数组索引代表26个字母
			//对于金信中的每一个字符都在数组中查找
	        //找到相应位减一,否则找不到返回false
			if(arr[temp] > 0) {
				arr[temp]--;
			}else {
				return false;
			}
		 }
		 
		 return true;
	 }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5719400.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存