Skip to content

Commit

Permalink
Added codes for 4 June
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jun 4, 2024
1 parent 061c686 commit a39cdea
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
42 changes: 42 additions & 0 deletions GeeksForGeeks/June/04-6-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/June/04-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
2 changes: 2 additions & 0 deletions LeetCode/June/04-6-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(1)
17 changes: 17 additions & 0 deletions LeetCode/June/04-6-24/Solution.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit a39cdea

Please sign in to comment.