diff --git a/GeeksForGeeks/June/04-6-24/GFG.java b/GeeksForGeeks/June/04-6-24/GFG.java new file mode 100644 index 0000000..2e95440 --- /dev/null +++ b/GeeksForGeeks/June/04-6-24/GFG.java @@ -0,0 +1,42 @@ +//{ Driver Code Starts +// Initial Template for Java +import java.io.*; +import java.util.*; + +class GfG { + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while (t-- > 0) { + String s = sc.next(); + Solution ob = new Solution(); + System.out.println(ob.binaryNextNumber(s)); + } + } +} +// } Driver Code Ends + + +// User function Template for Java + +class Solution { + String binaryNextNumber(String s) { + // code here. + StringBuilder sb = new StringBuilder(s); + StringBuilder sb1 = new StringBuilder("1"); + int i = sb.length()-1; + + while(i>=0 && sb.charAt(i) == '1') + { + sb.setCharAt(i, '0'); + i--; + } + if(i<0) + sb.insert(0,"1"); + else + sb.setCharAt(i, '1'); + + int index = sb.indexOf("1"); + return sb.substring(index).toString(); + } +} diff --git a/GeeksForGeeks/June/04-6-24/README.md b/GeeksForGeeks/June/04-6-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/June/04-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/June/04-6-24/README.md b/LeetCode/June/04-6-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/June/04-6-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/June/04-6-24/Solution.java b/LeetCode/June/04-6-24/Solution.java new file mode 100644 index 0000000..bdd6ed6 --- /dev/null +++ b/LeetCode/June/04-6-24/Solution.java @@ -0,0 +1,17 @@ +class Solution +{ + public int longestPalindrome(String s) + { + int ans = 0; + int[] count = new int[128]; + + for (char c : s.toCharArray()) + ++count[c]; + + for (int freq : count) + ans += freq % 2 == 0 ? freq : freq - 1; + + boolean hasOddCount = Arrays.stream(count).anyMatch(freq -> freq % 2 == 1); + return ans + (hasOddCount ? 1 : 0); + } +}