String Programmes
String Programmes
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];
}
}
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;
}
}
1.
class Solution {
public int minLength(String s) {
else{
st.push(s.charAt(i));
}
}
return st.size();
}
}
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 "";
return str1.substring(0,gcdDivider);
}
public int gcd(int x, int y){
if(y == 0) return x;
else{
return gcd(y, x%y);
}
}
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;
}
}