2023 E3 算法题第一题 (Difference Letter Count)

2024-04-15 03:12

本文主要是介绍2023 E3 算法题第一题 (Difference Letter Count),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题的内容

Task 1
You are given a string letters made of N English letters. Count the number of different letters that appear in both uppercase and lowercase where all lowercase occurrences of the given letter appear before any uppercase occurrence.
For example, for letters = “aaAbcCABBc” the answer is 2. The condition is met for letters ‘a’ and ‘b’, but not for ‘c’.
Write a function:
class Solution {    public int solution(String letters);    }
1 
that, given a string letters, returns the number of different letters fulfilling the conditions above.
Examples:Given letters = ''aaAbcCABBc", the function should return 2, as explained above.
Given letters = “xyzXYZabcABC”, the function should return 6.
Given letters = “ABCabcAefG”, the function should return 0.
Write an efficient algorithm for the following assumptions:N is an integer within the range [1…100,000];
string letters is made only of letters (a-z and/or A-Z).

解法 1 

思路 1

构建一个Map,Map的key是小写字母,Map的Value有三个值分别为0,1,2.  

0 : 代表只有一个小写字母。

1: 代表所有这个小写字母在大写字母之前。也就是正确的字母

2: 至少一个大写字母在这个小写字母前面。也就是不正确的字母。

写一个for 循环构建这个Map就可以了。

代码 1


class DifferenceLetterCount2 {public boolean isLowerCase(String letter){if (letter.equals(letter.toLowerCase())){return true;}else{return false;}}public int solution(String letters) {// 0 : initial , 1 : true, 2 : falseMap<String, Integer> letterMap = new HashMap<String, Integer>();Integer correctOrder = 0;char[] letterArray = letters.toCharArray();for (int i =0 ; i< letters.length(); i++ ){String letter = Character.toString(letterArray[i]);Integer flag = letterMap.get(letter.toLowerCase());if (flag== null){if (isLowerCase(letter)){letterMap.put(letter, 0);}else{letterMap.put(letter.toLowerCase(), 2);}}else{if (flag==0 ){if (!isLowerCase(letter)){correctOrder++;letterMap.put(letter.toLowerCase(), 1);}}else if(flag==1){if (!isLowerCase(letter)){letterMap.put(letter.toLowerCase(), 2);}}}}return correctOrder;}public static void main(String[] args) {DifferenceLetterCount2 solution = new DifferenceLetterCount2();// Test casesSystem.out.println(solution.solution("aaAbcCABBc"));  // Output: 2System.out.println(solution.solution("xyzXYZabcABC")); // Output: 6System.out.println(solution.solution("ABCabcAefG"));   // Output: 0}
}

解法 2 

思路

构建一个长度为26的整数数组。26代表26各字母,数组里面整数有三个值0, 1, -1, 2.

0 : 初始值。代表没有相应的字母。

1:只有一个小写字母。

2: 所有小写在大写之前,代表正确顺序。

-1 : 不正确顺序,至少一个大写字母在小写之前。

写个for 循环构建这个整数数组。

代码


class DifferenceLetterCount {public int solution(String letters) {int count = 0;int[] array = new int[26];for (int i = 0; i < letters.length(); i++) {if (Character.isLowerCase(letters.charAt(i))) { // lowerint index = letters.charAt(i) - 'a';// -1, 1 do nothingif (array[index] == 0)array[index] = 1; // only have lowercase letterif (array[index] == 2)array[index] = -1; //incorrect order} else { // upperint index = Character.toLowerCase(letters.charAt(i)) - 'a';// -1, 2 do nothingif (array[index] == 0)array[index] = -1; // incorrect orderelse if (array[index] == 1)array[index] = 2; // correct order}}for (int e : array) {if (e == 2)count++; // count the number of correct order.}return count;}public static void main(String[] args) {DifferenceLetterCount solution = new DifferenceLetterCount();// Test casesSystem.out.println(solution.solution("aaAbcCABBc"));  // Output: 2System.out.println(solution.solution("xyzXYZabcABC")); // Output: 6System.out.println(solution.solution("ABCabcAefG"));   // Output: 0}
}

这篇关于2023 E3 算法题第一题 (Difference Letter Count)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/904744

相关文章

据阿谱尔APO Research调研显示,2023年全球髓内钉市场销售额约为4.7亿美元

根据阿谱尔 (APO Research)的统计及预测,2023年全球髓内钉市场销售额约为4.7亿美元,预计在2024-2030年预测期内将以超过3.82%的CAGR(年复合增长率)增长。 髓内钉市场是指涉及髓内钉制造、分销和销售的行业。髓内钉是一种用于整形外科手术的医疗器械,用于稳定长骨骨折,特别是股骨、胫骨和肱骨。髓内钉通常由不銹钢或钛等材料制成,并插入骨的髓管中,以在愈合过程中提供结构支

代码随想录算法训练营:12/60

非科班学习算法day12 | LeetCode150:逆波兰表达式 ,Leetcode239: 滑动窗口最大值  目录 介绍 一、基础概念补充: 1.c++字符串转为数字 1. std::stoi, std::stol, std::stoll, std::stoul, std::stoull(最常用) 2. std::stringstream 3. std::atoi, std

人工智能机器学习算法总结神经网络算法(前向及反向传播)

1.定义,意义和优缺点 定义: 神经网络算法是一种模仿人类大脑神经元之间连接方式的机器学习算法。通过多层神经元的组合和激活函数的非线性转换,神经网络能够学习数据的特征和模式,实现对复杂数据的建模和预测。(我们可以借助人类的神经元模型来更好的帮助我们理解该算法的本质,不过这里需要说明的是,虽然名字是神经网络,并且结构等等也是借鉴了神经网络,但其原型以及算法本质上还和生物层面的神经网络运行原理存在

大林 PID 算法

Dahlin PID算法是一种用于控制和调节系统的比例积分延迟算法。以下是一个简单的C语言实现示例: #include <stdio.h>// DALIN PID 结构体定义typedef struct {float SetPoint; // 设定点float Proportion; // 比例float Integral; // 积分float Derivative; // 微分flo

LeetCode 算法:二叉树的中序遍历 c++

原题链接🔗:二叉树的中序遍历 难度:简单⭐️ 题目 给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。 示例 1: 输入:root = [1,null,2,3] 输出:[1,3,2] 示例 2: 输入:root = [] 输出:[] 示例 3: 输入:root = [1] 输出:[1] 提示: 树中节点数目在范围 [0, 100] 内 -100 <= Node.

【Java算法】滑动窗口 下

​ ​    🔥个人主页: 中草药 🔥专栏:【算法工作坊】算法实战揭秘 🦌一.水果成篮 题目链接:904.水果成篮 ​ 算法原理 算法原理是使用“滑动窗口”(Sliding Window)策略,结合哈希表(Map)来高效地统计窗口内不同水果的种类数量。以下是详细分析: 初始化:创建一个空的哈希表 map 用来存储每种水果的数量,初始化左右指针 left

ROS2从入门到精通4-4:局部控制插件开发案例(以PID算法为例)

目录 0 专栏介绍1 控制插件编写模板1.1 构造控制插件类1.2 注册并导出插件1.3 编译与使用插件 2 基于PID的路径跟踪原理3 控制插件开发案例(PID算法)常见问题 0 专栏介绍 本专栏旨在通过对ROS2的系统学习,掌握ROS2底层基本分布式原理,并具有机器人建模和应用ROS2进行实际项目的开发和调试的工程能力。 🚀详情:《ROS2从入门到精通》 1 控制插

算法与数据结构面试宝典——回溯算法详解(C#,C++)

文章目录 1. 回溯算法的定义及应用场景2. 回溯算法的基本思想3. 递推关系式与回溯算法的建立4. 状态转移方法5. 边界条件与结束条件6. 算法的具体实现过程7. 回溯算法在C#,C++中的实际应用案例C#示例C++示例 8. 总结回溯算法的主要特点与应用价值 回溯算法是一种通过尝试各种可能的组合来找到所有解的算法。这种算法通常用于解决组合问题,如排列、组合、棋盘游

【图像识别系统】昆虫识别Python+卷积神经网络算法+人工智能+深度学习+机器学习+TensorFlow+ResNet50

一、介绍 昆虫识别系统,使用Python作为主要开发语言。通过TensorFlow搭建ResNet50卷积神经网络算法(CNN)模型。通过对10种常见的昆虫图片数据集(‘蜜蜂’, ‘甲虫’, ‘蝴蝶’, ‘蝉’, ‘蜻蜓’, ‘蚱蜢’, ‘蛾’, ‘蝎子’, ‘蜗牛’, ‘蜘蛛’)进行训练,得到一个识别精度较高的H5格式模型文件,然后使用Django搭建Web网页端可视化操作界面,实现用户上传一

【数据结构与算法 经典例题】使用队列实现栈(图文详解)

💓 博客主页:倔强的石头的CSDN主页               📝Gitee主页:倔强的石头的gitee主页    ⏩ 文章专栏:《数据结构与算法 经典例题》C语言                                   期待您的关注 ​​ 目录  一、问题描述 二、前置知识 三、解题思路 四、C语言实现代码 🍃队列实现代码: