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. vue+element-ui 实现table单元格点击编辑,并且按上下左右键单元格之间切换

    通过我的测试我发现两个两种方法来编辑单元格的内容 第一种点击编辑: 我是给td里添加一个input,将值赋值给input,当失去焦点的时候将input的值付给td原来的内容,然后将input删除, 代 ...

  2. order pick-up and delivery problem

    问题一: pi表示取第i个单,di表示送第i个单.di不能在pi的前面.给一个取单送单的顺序,问是否是valid顺序. public boolean isValidOrderList(List< ...

  3. Python豆瓣源镜像

    pip install pymysql -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

  4. C++Primer 5th Chap7 Classes

    this关键字: 在成员函数内部可以直接调用函数的对象的成员(类成员的直接访问看做是对this隐式引用,默认this指向非常量) 例如:string isbn() const{return this- ...

  5. php实现微信小程序登录

    以上是官方的流程介绍,已经说的很详细了,现在简单介绍一下流程 前端通过wx.login生成code传递给后端,后端通过提交Appid + appSecret + code 到微信方服务器 获取 ses ...

  6. python学习-23 函数

    函数 1.函数分为:数学定义的函数和编程语言中的函数 例如: - 数学定义的函数:y=2*x+1 - 编程语言的函数: def test(x): x += 1 return x def  :定义函数的 ...

  7. 数据仓库之抽取数据:openrowset函数带bulk操作符的用法

    原文:数据仓库之抽取数据:openrowset函数带bulk操作符的用法 在做数据仓库时,最重要的就是ETL的开发,而在ETL开发中的第一步,就是要从原OLTP系统中抽取数据到过渡区中,再对这个过渡区 ...

  8. Junit 学习1 junit的简单使用

    package junit; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; impo ...

  9. js之拖拽事件

    js之拖拽事件 api:https://www.runoob.com/jsref/event-ondrag.html 拖拽事件是js原生的事件,使用时在div上添加 draggable="t ...

  10. Vue路由相关配置

    什么是路由? 1.在以前页面跳转使用的是超链接a标签或者js location.href,而路由是跳转切换组件的跳转方式 2.路由就是监听url的改变并提供相对应的组件用于展示 3.vue-route ...