-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
061c686
commit a39cdea
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |