g2901_3000.s2957_remove_adjacent_almost_equal_characters.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g2901_3000.s2957_remove_adjacent_almost_equal_characters;
// #Medium #String #Dynamic_Programming #Greedy
// #2024_01_15_Time_1_ms_(100.00%)_Space_42.1_MB_(15.57%)
public class Solution {
public int removeAlmostEqualCharacters(String word) {
int count = 0;
char[] wordArray = word.toCharArray();
for (int i = 1; i < wordArray.length; i++) {
if (Math.abs(wordArray[i] - wordArray[i - 1]) <= 1) {
count++;
wordArray[i] =
(i + 1 < wordArray.length
&& (wordArray[i + 1] != 'a' && wordArray[i + 1] != 'b'))
? 'a'
: 'z';
}
}
return count;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy