Maximum Odd Binary Number - LeetCode
Can you solve this real interview question? Maximum Odd Binary Number - You are given a binary string s that contains at least one '1'. You have to rearrange the bits in such a way that the resulting binary number is the maximum odd binary number that can
leetcode.com
풀이
class Solution {
public String filterBySubString(String originString, String subString) {
BinaryOperator<String> concatOperator = (partialString, element) -> partialString + element;
return originString.chars()
.mapToObj(c -> String.valueOf((char) c))
.filter(c -> c.contains(subString))
.reduce("", concatOperator);
}
public String maximumOddBinaryNumber(String s) {
String oneString = filterBySubString(s, "1");
String zeroString = filterBySubString(s, "0");
String ans = oneString + zeroString;
return ans.substring(1) + "1";
}
}
'알고리즘 > 릿코드' 카테고리의 다른 글
[Leetcode] - Remove Nth Node From End of List (0) | 2024.03.03 |
---|---|
[Leetcode] Jump Game IV (0) | 2023.03.05 |