c# - C#如何找到字符串中的最后一个唯一字母,并获取它的索引?
问题描述
以下是我要解决的方法.....
public int IndexOfLastUniqueLetter(string str)
我还没有尝试过任何代码,因为我不知道如何解决它。
请告知如何解决此方法以及执行此操作的代码。谢谢,麻烦您了。
解决方案
The algorithm boils down to these two steps:
- Find which letters are unique,
- Find the last of these unique letters in the original string.
Counting occurrences of each letter could be done with a loop, or with an associative container, depending on your preferences. If count is 1
, the letter is unique; otherwise, it is not unique. Finding last-of could be done by walking the string backward.
The only thing to worry about here is what to do when there are no unique letters in the string (e.g. "aaaa"
or "abab"
). Your code should detect this condition explicitly, and return -1
.
推荐阅读
- django - 如何在 django rest 框架中的 simplejwt 令牌身份验证中跳过或删除密码字段?
- sql-server - 使用 Bash 脚本更新本地 SQL Server
- html - 导航栏不会折叠到汉堡菜单
- python - 数据框计算
- sql-server - SQL Server 如何停止从 xp_cmdshell 运行的进程
- c - 无法验证 TCP 校验和
- sap - 有谁知道如何在 Automation Anywhere 11 中自动化 SAP 登录
- generics - 如何在 gson typeToken 中使用泛型类型?
- twilio - 使用 twilio 子帐户调用
- css - 尝试使用 React-JSS 实现动态值