String Programmes

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

2697.

Lexicographically Smallest Palindrome

1.
class Solution {
public String makeSmallestPalindrome(String s) {
char[] arr = s.toCharArray();
for (int i = 0, n = arr.length; i < n/2; ++i)
arr[i] = arr[n-1-i] = (char) Math.min(arr[i], arr[n-1-i]);
return String.valueOf(arr);
}
}

2.
class Solution {
public String makeSmallestPalindrome(String s) {
char[] str = s.toCharArray();

int n = s.length();

for(int i=0;i<n/2;i++){
if(str[i] > str[n-i-1]){
str[i] = str[n-i-1];
} else {
str[n-i-1] = str[i];
}
}

StringBuilder sb = new StringBuilder();


for(char ch : str)
sb.append(ch);

return sb.toString();
}
}

3.
class Solution {
public String makeSmallestPalindrome(String s) {

int i=0;

char[] ch=s.toCharArray();
int j=ch.length-1;

while(i<j)
{
if(ch[i]==ch[j])
{
i++;
j--;
}
else if(ch[i]<ch[j])
{
ch[j]=ch[i];
i++;
j--;
}
else
{
ch[i]=ch[j];
i++;
j--;
}
}
String str=new String(ch);
return str;
}
}

2696. Minimum String Length After Removing Substrings

1.
class Solution {
public int minLength(String s) {

Stack<Character> st=new Stack<>();


int n=s.length();
st.push(s.charAt(0));
for(int i=1;i<n;i++)
{
if(!st.isEmpty()&&((st.peek()=='A'&&s.charAt(i)=='B')||
(s.charAt(i)=='D'&&st.peek()=='C')))
{
st.pop();
}

else{
st.push(s.charAt(i));
}
}
return st.size();
}
}

30. Substring with Concatenation of All Words


class Solution {
public List<Integer> findSubstring(String s, String[] words) {
int n = words.length;
HashMap<String, Integer> map = new HashMap<>();
List<Integer> ans = new ArrayList<>();
for (int i = 0; i < n; i++) {
map.put(words[i],map.getOrDefault(words[i],0)+1);
}
int k = words[0].length() * words.length;
int z = words[0].length();
int i = 0, j = 0;
while (j < s.length()) {
if (j - i + 1 == k) {
String sub = s.substring(i, j + 1);
HashMap<String, Integer> map2 = new HashMap<>();
int p = 0;
while (p < sub.length()) {
String temp = sub.substring(p, p + z);
map2.put(temp,map2.getOrDefault(temp,0)+1);
p +=z;
}
if (map.equals(map2)){
ans.add(i);
}
i++;
}
j++;
}
return ans;
}
}

1071. Greatest Common Divisor of Strings

class Solution {
public String gcdOfStrings(String str1, String str2) {
int x=str1.length();
int y=str2.length();
// int z=Math.min()

String s=str1+str2;
String t=str2+str1;
if(!(s.equalsIgnoreCase(t)))
return "";

while(x!=y)
{
if(x>y)
x=x-y;
else
y=y-x;

}
String str3=str2.substring(0,x);
return str3;

}
}

class Solution {
public String gcdOfStrings(String str1, String str2) {
int s1 =str1.length(), s2= str2.length();
if(!(str1 + str2).equals(str2 + str1)) return "";

int gcdDivider = gcd(s1,s2);

return str1.substring(0,gcdDivider);
}
public int gcd(int x, int y){
if(y == 0) return x;
else{
return gcd(y, x%y);
}
}

1431. Kids With the Greatest Number of Candies

class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
int max=Integer.MIN_VALUE;
int len=candies.length;
List<Boolean> ls=new ArrayList<>();

for(int i=0;i<candies.length;i++)
{
max=Math.max(max,candies[i]);
}
for(int j=0;j<candies.length;j++)
{
if(candies[j]+extraCandies>=max)
ls.add(true);
else{
ls.add(false);
}

}
return ls;

}
}

class Solution {
public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) {
ArrayList<Boolean> ans = new ArrayList<>();
int max = 0;
for(int i = 0; i< candies.length; i++){
if( candies[i] >= max){
max= candies[i];
} }
for( int kids: candies){
if(kids + extraCandies >= max){
ans.add(true);
} else {

ans.add(false);
}
}
return ans;
}
}

int count=0;
int[] f=new int[flowerbed.length];
if(n==0)
{
return true;
}
for(int i=0;i<f.length;i++)
{
if(f[i]==0&&(i==0||f[i-1]==0)&&(i==f.length-1||f[i+1]==0))
{
f[i]=1;
count++;
if(count==n)
return true;
}
}
return false;

}
}

class Solution {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
if (n == 0) {
return true;
}
for (int i = 0; i < flowerbed.length; i++) {
if (flowerbed[i] == 0 && (i == 0 || flowerbed[i-1] == 0) && (i ==
flowerbed.length-1 || flowerbed[i+1] == 0)) {
flowerbed[i] = 1;
n--;
if (n == 0) {
return true;
}
}
}
return false;

////////////////////////////////////////////////////////////
345. Reverse Vowels of a String

class Solution {
public String reverseVowels(String s) {

int i=0,j=s.length()-1;
char ch;
while(i<j){
if(s.charAt(i).matcher("[aeiou]")&&s.charAt(j).matcher("[aeiou]"))
{
char ch=s.charAt(i);
s.replace(s.charAt(i),s.charAr(j));
s.replace(s.charAt(j),ch);
i++;j--;
// s.charAt(i);=s.charAt(j);
// s.charAt(j)=s.charAt(i);
}
else
if(s.charAt(i).matcher("[^aeiou]")&&s.charAt(j).matcher("[^aeiou]")){
i++;j--;
}

else if(s.charAt(i).matcher("[^aeiou]")){
i++;
}
else{
j--;
}
}
return s;

}
}

class Solution {
public String reverseVowels(String s) {
int i=0;
int j=s.length()-1;
char[] ch=s.toCharArray();
char temp;
while(i<j)
{
if(!isVowel(ch[i])){
i++;
}
else if(!isVowel(ch[j]))
{
j--;
}
else{
temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
i++;
j--;
}
}
String str=new String(ch);
return str;
}
public static boolean isVowel(char c){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||
c=='O'||c=='U'){
return true;
}
else{
return false;
}
}

You might also like