How to find First Non-Repeated Character from String
You need to write a function, which will accept a String and return first non-repeated character, for example in the world "hello", except 'l' all are non-repeated, but 'h' is the first non-repeated character. Similarly, in word "swiss" 'w' is the first non-repeated character. One way to solve this problem is creating a table to store count of each character, and then picking the first entry which is not repeated. The key thing to remember is order, your code must return first non-repeated letter.
public class solution {
public char firstNonRepeating(String s) {
char result = '#';
if (s == null || s.length == 0) {
return result;
}
Map<Character,Integer> map = HashMap<>();
for (int i = 0; i < s.length(); i++) {
if (map.containsKey(s.charAt(i))) {
map.put(s.charAt(i), map.get(s.charAt(i)) + 1);
} else {
map.put(s.charAt(i), 1);
}
}
for (int i = 0; i < s.length; i++) {
if (map.get(s.charAt(i) == 1)) {
result = s.charAt(i);
return result;
}
}
return result;
}
}
How to find First Non-Repeated Character from String的更多相关文章
- LeetCode 1156. Swap For Longest Repeated Character Substring
原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...
- 【leetcode】1156. Swap For Longest Repeated Character Substring
题目如下: Given a string text, we are allowed to swap two of the characters in the string. Find the leng ...
- Java_字符类(Character、String、StringBuffer)_char是基本数据类型,Character是其包装类型。
在java中有三个类负责对字符的操作:Character.String.StringBuffer.其中,Character类是对单个字符进行操作,String是对一个字符序列的操作,Stri ...
- SyntaxError: JSON.parse: bad control character in string literal at line 1 column 16 of the JSON data
JSON.parse转化Json字符串时出现:SyntaxError: JSON.parse: bad control character in string literal at line 1 co ...
- java基础系列(一):Number,Character和String类及操作
这篇文章总结了Java中最基础的类以及常用的方法,主要有:Number,Character,String. 1.Number类 在实际开发的过程中,常常会用到需要使用对象而不是内置的数据类型的情形.所 ...
- Eclipse 出现select type (? = any character,*= any String,Tz=TimeZone)
在eclipse中想运行project的时候,往往是右键项目名称---->run As --->Java Application 但是会弹出窗口显示select type (? = any ...
- 187. Repeated DNA Sequences (String; Bit)
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- java 中的Number类 Character类 String类 StringBuffer类 StringBuilder类
1. Number类 Java语言为每一个内置数据类型提供了对应的包装类.所有的包装类(Integer.Long.Byte.Double.Float.Short)都是抽象类Number的子类.这种由编 ...
- Java - replace a character at a specific index in a string?
String are immutable in Java. You can't change them. You need to create a new string with the charac ...
随机推荐
- GC(Garbage Collection)
GC(Garbage Collection) GC背景 创建对象会消耗内存,如果不回收对象占用的内存,内存使用率会越来越高,最终出现OutOfMemoryError(OOM) 在C++中专 ...
- 剑指offer53:表示数值的字符串,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是
1 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3 ...
- UOJ208 UOIP十合一(提交答案)
首先对每张图都去掉自环. 1:给出的就是DAG.答案即为2m. 2.5:显然每个SCC之间互相独立.这两个点都满足SCC中的点很少.于是对每个SCC暴力枚举边集判环,而SCC之间的边显然选不选没有影响 ...
- Http 与 Https区别
传统Http协议弊端 传统Http协议弊端是明文的,如果别人采用抓包分析可以获取到明文数据. 什么是Https协议 HTTPS(Hyper Text Transfer Protocol over Se ...
- eclipse智能提示报错(to avoid the message, disable the...)
然后这里可能会再报别的错误 我的做法是 再重新将 code recommenders 打开 ********************************** 为什么会出现 这样错误呢 ? 简 ...
- CCF 2017-09-2 公共钥匙盒
CCF 2017-09-2 公共钥匙盒 题目 问题描述 有一个学校的老师共用N个教室,按照规定,所有的钥匙都必须放在公共钥匙盒里,老师不能带钥匙回家.每次老师上课前,都从公共钥匙盒里找到自己上课的教室 ...
- Angular 惰性路由
根路由上的一个 loadChildren 属性,设置为一个字符串.这样就是惰性路由了. angular6 这样写:loadChildren: './background-check/backgroun ...
- C++ raw string literal
raw string literal 以 R"( 开头, )" 结束,是可以跨越多行的字符串字面值,转义字符如 \t\n 在raw string literal中是普通的文本 ...
- KVM之配置桥接网卡
配置桥接网卡 添加桥接网卡br0,注释掉已有的eth0配置 [root@ubuntu01 ~]# vi /etc/network/interfaces # This file describes th ...
- Java中程序、进程、线程的区别。
程序.进程.线程的区别. 程序(program):是一个指令的集合.程序不能独立执行,只有被加载到内存中,系统为他分配资源后才能执行. 进程(process):一个执行中的程序称为进程. 进程是系统分 ...