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的更多相关文章

  1. LeetCode 1156. Swap For Longest Repeated Character Substring

    原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a str ...

  2. 【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 ...

  3. Java_字符类(Character、String、StringBuffer)_char是基本数据类型,Character是其包装类型。

         在java中有三个类负责对字符的操作:Character.String.StringBuffer.其中,Character类是对单个字符进行操作,String是对一个字符序列的操作,Stri ...

  4. 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 ...

  5. java基础系列(一):Number,Character和String类及操作

    这篇文章总结了Java中最基础的类以及常用的方法,主要有:Number,Character,String. 1.Number类 在实际开发的过程中,常常会用到需要使用对象而不是内置的数据类型的情形.所 ...

  6. Eclipse 出现select type (? = any character,*= any String,Tz=TimeZone)

    在eclipse中想运行project的时候,往往是右键项目名称---->run As --->Java Application 但是会弹出窗口显示select type (? = any ...

  7. 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 ...

  8. java 中的Number类 Character类 String类 StringBuffer类 StringBuilder类

    1. Number类 Java语言为每一个内置数据类型提供了对应的包装类.所有的包装类(Integer.Long.Byte.Double.Float.Short)都是抽象类Number的子类.这种由编 ...

  9. 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 ...

随机推荐

  1. ARTS 第八周打卡

    Algorithm : 做一个 leetcode 的算法题 13. 罗马数字转整数 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符          数值 I         ...

  2. 解读PHP面试-高并发解决方案类考察点

    整理自慕课网360大牛全面解读PHP面试 ,购买链接:https://coding.imooc.com/class/133.html 1.高并发和大流量解决方法 真题回顾 PHP如何解决高并发大流量问 ...

  3. IMPDPORA-27046,dump文件损坏

    客户提出导入报错 一.报错如下 SYMPTOMS DataPump Import (IMPDP) fails with the following errors: ORA-: invalid oper ...

  4. (八)mybatis之多对多

    一.需求分析 需求:查询所有用户的信息以及每个用户所属的组的信息 分析:一个用户可以有多个组,一个组也可以由多个用户. 多对多,可以设置一张中间表,该表存放的是用户表和组表的对应关系. 二.创建数据库 ...

  5. MySQL INNER JOIN子句介绍

    MySQL INNER JOIN子句介绍 MySQL INNER JOIN子句将一个表中的行与其他表中的行进行匹配,并允许从两个表中查询包含列的行记录. INNER JOIN子句是SELECT语句的可 ...

  6. java jdk1.8 API

    里面有 中英文 jdk 1.8 API    还有 jdk1.6 和1.7 英文 API 链接:https://pan.baidu.com/s/1tchABVX7htJCaO3quENP1g提取码:y ...

  7. element-ui重置表单并清除校验的方法

    this.$refs['activityForm'].resetFields(); 只会重置之前表单的内容,并不会清空 只需在关闭弹框的cancel方法中写上重置表单的方法即可 cancel() { ...

  8. Ubuntu安装telnet

    安装 # sudo apt-get install xinetd telnetd 配置 -> 加入以下选项 # sudo vi /etc/inetd.conf telnet stream tcp ...

  9. zookeeper:2

    单机环境下安装: 下载地址:http://apache.fayea.com/zookeeper/stable/ 解压zookeeper :tar -zxvf zookeeper-3.4.10.tar. ...

  10. Endless looping of packets in TCP/IP networks (Routing Loops)

    How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...