Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How does LeetCode replace all question marks

Shulou Source: shulou.com Published: 2022-06-01 13:06:04 09月29日 Update

This article will explain in detail how to replace all question marks with LeetCode. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Title

Give you one that contains only lowercase letters and'?' Character string s, please put all the'?' Convert to several lowercase letters so that the final string does not contain any consecutive repeated characters.

The question test case is guaranteed to be divided by'?' There are no consecutive repeating characters other than characters.

Returns the final string after all conversions are completed (no conversion may be required). If there is more than one solution, return to any one of them. It can be proved that, under given constraints, the answer always exists.

Train of thought

Because I encountered the problem of string replacement in the competition two days ago, I saw this and discussed it with my classmates.

Store the string in an array, traverse it, and determine whether it is'?'

If the current element is the first element and the only element, replace it directly with'a'

If it is not unique, replace it with a different lowercase letter from the latter one

If the current element is the last element, replace it with a different lowercase letter from the previous one

If the current element is in the middle of a string, replace it with lowercase letters that are not the same as before and after

The process of replacement: start with'a'to determine whether the newChar is the same as the characters before / after it, and if so, it will be postponed.

Code public String modifyString (String s) {if (s = = "") {return "the current string is empty, please enter it correctly!" ;} char [] c = s.toCharArray (); for (int I = 0; I

< s.length(); i++) { if(c[i] == '?') { char newChar = 'a'; while ((i>

0 & & c [I-1] = = newChar) | | (I

Tags: Characters strings elements letters lowercase different same article input output question mark special more good practical three code location content classmate Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Information Xiaomi Docker Apple