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 return the first character that meets the criteria

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

Share

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

This article mainly introduces "how to return the first character that meets the criteria". In daily operation, I believe many people have doubts about how to return the first character that meets the requirements. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubts of "how to return the first character that meets the criteria". Next, please follow the editor to study!

Problem description

For a given string s, you need to write an algorithm that returns unrepeated characters in the given string.

This question came out again in the subsequent interview.

This time you need the found string returned by the function, and there is also case in the input string.

In addition, because of the limitations of online compilers, you can't use HashMap.

Problem-solving ideas

Using Java is relatively easy to handle.

The idea of solving the problem is also relatively simple, you need to use an intermediate variable to store, first of all, you need to convert the processed string into an array of char.

Then get the first character in the array.

When you get the first character, you do something like this by replacing the character with the target string.

If there is the same one, it will certainly be replaced, and you will consider replacing the uppercase one and the lowercase one again.

If there is the same capital letter, it will also be replaced.

For example, the string "serTSSEr", after you complete the above algorithm, suppose we compare the first character to be replaced is s, then the string after the completion of the algorithm is "erTEr".

We find that the length of the string is not the original length-1, because you have replaced multiple strings, so you can know that the character being found is duplicated.

When we loop to the character T, we will find that the string length after the completion of the algorithm is the original input string length-1, then we know that T is the character we need to output.

It is important to pay attention to the special case of "ssee". If you loop to the end, you may find that the length of the original character and the length of the character after the completion of the whole loop have not changed, then it means that all characters are repeated, then you should return "".

further more

In order to reduce the number of searches, you can do algorithmic search and replacement in the remaining strings after the first replacement, because this algorithm value requires finding characters and does not require you to output subscript.

So in the loop, the length of the string you need to find next time is reduced, and the algorithm is more efficient.

For the complete test code, please refer to the GitHub link address in the question: https://github.com/cwiki-us-docs/java-tutorials/blob/master/toolkits/codebank/src/test/java/com/ossez/toolkits/codebank/tests/leetcode/LeetCode0387FirstUniqueCharacterTest.java

We will write this test method below for the reference of children's shoes.

/ * Return the firstUniqCharString without using Map * @ param data * @ return * / private String firstUniqCharString (String data) {/ / NULL CHECK if (data.equals (")) {return";} char [] strArray = data.toCharArray (); String retStr = ""; if (data.length () = = 1) {retStr = data;} for (int I = 0; I < strArray.length String rData +) {String valStr = Character.toString (strArray [I]); String rData = data; rData = data.replace (valStr, ""); rData = rData.replace (valStr.toUpperCase (Locale.ROOT), ""); rData = rData.replace (valStr.toLowerCase (Locale.ROOT), "); if (rData.length () = 0) {retStr =" } else if (rData.length () + 1 = = data.length ()) {retStr = valStr; break;}} return retStr;} at this point, the study on "how to return the first character that meets the criteria" is over. I hope I can 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