Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write code to find the first non-repeating character in a string

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to write code to find the first non-repeating character in a string". In daily operations, I believe many people have doubts about how to write code to find the first non-repeating character in a string. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "how to write code to find the first non-repeating character in a string"! Next, please follow the editor to study!

Problem description

For a given string s, you need to write an algorithm that returns the position of non-repeating characters in the given string (index). If all characters are repeated in the given string, then you should return-1.

Sample

An example of this problem is given below for your reference.

Input: s = "comossez" 0Input: s = "lovelycomossez" 2Input: s = "aabb"-1 ideas comments and source code

On the whole, this topic is not very difficult.

There are many ways to solve the problem. first of all, you need to open the string into an array so that you can traverse character by character.

My idea is certainly not the most efficient, my idea is to put the string into the array, and then traverse the array, in the process also define a Map, the Key stored in this Map is the string being found, if the current string is not in the Map, then Put into it.

The Key performed by Put is the current string, and the value is the subscript of the array in which the current string resides.

If the current string already exists in Map, we can change the value of Map to 2: 5, using the # sign or any special character in the middle.

When we finish the above traversal, we have the map we need.

Then iterate through the Map to find the first value that does not contain the # sign.

For ordered storage, we need to use LinkedHashMap because HashMap is unordered, and unordered Map will get the input order in the wrong order when the first one is found.

The image above shows the value of the initialized Map after analyzing the memory.

At this point, the study on "how to write code to find the first non-repeating character in a string" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report