lionelsamrat10 / LeetCode-Solutions

Contains the solutions of all the problems, I am solving on Leetcode.
https://github.com/lionelsamrat10/LeetCode-Solutions
MIT License
2 stars 1 forks source link

1544. Make The String Great #52 #86

Closed Karthiks08 closed 3 years ago

Karthiks08 commented 3 years ago

class Solution {

public String makeGood(String s) {
    if(s.length()<=1)
        return s;
    int i=0, j=1;
    StringBuilder sb = new StringBuilder(s);
    while(j < sb.length() && sb.length() > 1) {
        if(Math.abs(sb.charAt(i) - sb.charAt(j)) == 32) {
            sb.deleteCharAt(i);
            sb.deleteCharAt(i);
            if(i>0)
                i = i-1;
            j = i+1;
            continue;
        }
        i++;j++;
    }
    return sb.toString();
}

}