codeparrot/xlcost-text-to-code huggingface dataset:
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated | Java implementation of the above approach ; Function that returns true if the sub - String starting from index i and ending at index j is a palindrome ; Function to get the required index ; Start comparing the two Strings from both ends . ; Break from the loop at first mismatch ; If it is possible to concatenate the Strings to form palindrome , return index ; If remaining part for s2 is palindrome ; If remaining part for s1 is palindrome ; If not possible , return - 1 ; Driver Code
class GFG { static boolean isPalindrome ( String s , int i , int j ) { while ( i < j ) { if ( s . charAt ( i ) != s . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } static int getIndex ( String s1 , String s2 , int len ) { int i = 0 , j = len - 1 ; while ( i < j ) { if ( s1 . charAt ( i ) != s2 . charAt ( j ) ) { break ; } i ++ ; j -- ; } if ( i == j ) { return i - 1 ; } else if ( isPalindrome ( s2 , i , j ) ) return i - 1 ; else if ( isPalindrome ( s1 , i , j ) ) return j ; return - 1 ; } public static void main ( String args [ ] ) { String s1 = " abcdf " , s2 = " sfgba " ; int len = s1 . length ( ) ; System . out . println ( getIndex ( s1 , s2 , len ) ) ; } }
Acronym words | Java implementation of the approach ; Function to return the number of strings that can be an acronym for other strings ; Frequency array to store the frequency of the first character of every string in the array ; To store the count of required strings ; Current word ; Frequency array to store the frequency of each of the character of the current string ; Check if the frequency of every character in the current string is <= its value in freq [ ] ; First character of the current string ; Driver code
class GFG { static int count_acronym ( int n , String [ ] arr ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] . charAt ( 0 ) - ' a ' ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String st = arr [ i ] ; int [ ] num = new int [ 26 ] ; for ( int j = 0 ; j < st . length ( ) ; j ++ ) num [ st . charAt ( j ) - ' a ' ] ++ ; boolean flag = true ; for ( int j = 1 ; j < 26 ; j ++ ) { if ( num [ j ] > freq [ j ] ) { flag = false ; break ; } } int x = st . charAt ( 0 ) - ' a ' ; if ( freq [ x ] - 1 < num [ x ] ) flag = false ; if ( flag ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { String [ ] arr = { " abc " , " bcad " , " cabd " , " cba " , " dzzz " } ; int n = arr . length ; System . out . println ( count_acronym ( n , arr ) ) ; } }
Sub | Java implementation of the approach ; Function that returns true if every lowercase character appears atmost once ; every character frequency must be not greater than one ; Function that returns the modified good string if possible ; If the length of the string is less than n ; To store frequency of each character ; Sub - strings of length 26 ; Get the frequency of each character in the current sub - string ; Check if we can get sub - string containing all the 26 characters ; Find which character is missing ; Fill with missing characters ; Find the next missing character ; Return the modified good string ; Driver code
import java . util . * ; class GFG { static boolean valid ( int [ ] cnt ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( cnt [ i ] >= 2 ) return false ; } return true ; } static String getGoodString ( String ss , int n ) { char [ ] s = ss . toCharArray ( ) ; if ( n < 26 ) return " - 1" ; int [ ] cnt = new int [ 27 ] ; for ( int i = 25 ; i < n ; i ++ ) { for ( int j = i ; j >= i - 25 ; j -- ) { if ( s [ j ] != ' ? ' ) cnt [ ( ( int ) s [ j ] - ( int ) ' a ' ) ] ++ ; } if ( valid ( cnt ) ) { int cur = 0 ; while ( cnt [ cur ] > 0 ) cur ++ ; for ( int j = i - 25 ; j <= i ; j ++ ) { if ( s [ j ] == ' ? ' ) { s [ j ] = ( char ) ( cur + ( int ) ( ' a ' ) ) ; cur ++ ; while ( cnt [ cur ] > 0 ) cur ++ ; } } return new String ( s ) ; } } return " - 1" ; } public static void main ( String [ ] args ) { String s = " abcdefghijkl ? nopqrstuvwxy ? " ; int n = s . length ( ) ; System . out . println ( getGoodString ( s , n ) ) ; } }
Modify the string by swapping continuous vowels or consonants | Java implementation of the above approach ; Function to check if a character is a vowel ; Function to swap two consecutively repeated vowels or consonants ; Traverse through the length of the string ; Check if the two consecutive characters are vowels or consonants ; swap the two characters ; Driver code
class GFG { static boolean isVowel ( char c ) { c = Character . toLowerCase ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { return true ; } return false ; } static String swapRepeated ( char str [ ] ) { for ( int i = 0 ; i < str . length - 1 ; i ++ ) { char c = 0 ; if ( ( isVowel ( str [ i ] ) && isVowel ( str [ i + 1 ] ) ) || ( ! isVowel ( str [ i ] ) && ! isVowel ( str [ i + 1 ] ) ) ) { c = str [ i ] ; str [ i ] = str [ i + 1 ] ; str [ i + 1 ] = c ; } } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( swapRepeated ( str . toCharArray ( ) ) ) ; } }
Find the lexicographically largest palindromic Subsequence of a String | Java program to find the largest palindromic subsequence ; Function to find the largest palindromic subsequence ; Find the largest character ; Append all occurrences of largest character to the resultant string ; Driver Code
class GFG { static String largestPalinSub ( String s ) { String res = " " ; char mx = s . charAt ( 0 ) ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) mx = ( char ) Math . max ( ( int ) mx , ( int ) s . charAt ( i ) ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == mx ) res += s . charAt ( i ) ; return res ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( largestPalinSub ( s ) ) ; } }
Generate lexicographically smallest string of 0 , 1 and 2 with adjacent swaps allowed | Java implementation of the approach ; Function to print the required string ; count number of 1 s ; To check if the all the 1 s have been used or not ; Print all the 1 s if any 2 is encountered ; If str [ i ] = 0 or str [ i ] = 2 ; If 1 s are not printed yet ; Driver code
class GFG { static void printString ( char [ ] str , int n ) { int ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == '1' ) ones ++ ; boolean used = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '2' && ! used ) { used = true ; for ( int j = 0 ; j < ones ; j ++ ) System . out . print ( "1" ) ; } if ( str [ i ] != '1' ) System . out . print ( str [ i ] ) ; } if ( ! used ) for ( int j = 0 ; j < ones ; j ++ ) System . out . print ( "1" ) ; } public static void main ( String [ ] args ) { String str = "100210" ; int n = str . length ( ) ; printString ( str . toCharArray ( ) , n ) ; } }
K length words that can be formed from given characters without repetition | Java implementation of the approach ; Function to return the required count ; To store the count of distinct characters in str ; Traverse str character by character ; If current character is appearing for the first time in str ; Increment the distinct character count ; Update the appearance of the current character ; Since P ( n , r ) = n ! / ( n - r ) ! ; Return the answer ; Driver code
import java . util . * ; class solution { static int findPermutation ( String str , int k ) { boolean [ ] has = new boolean [ 26 ] ; Arrays . fill ( has , false ) ; int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ! has [ str . charAt ( i ) - ' a ' ] ) { cnt ++ ; has [ str . charAt ( i ) - ' a ' ] = true ; } } int ans = 1 ; for ( int i = 2 ; i <= cnt ; i ++ ) ans *= i ; for ( int i = cnt - k ; i > 1 ; i -- ) ans /= i ; return ans ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int k = 4 ; System . out . println ( findPermutation ( str , k ) ) ; } }
Find the number in a range having maximum product of the digits | Java Program to find the number in a range having maximum product of the digits ; Returns the product of digits of number x ; This function returns the number having maximum product of the digits ; Converting both integers to strings string a = l . ToString ( ) ; ; Let the current answer be r ; Stores the current number having current digit one less than current digit in b ; Replace all following digits with 9 to maximise the product ; Convert string to number ; Check if it lies in range and its product is greater than max product ; Driver Code
class GFG { static int product ( int x ) { int prod = 1 ; while ( x > 0 ) { prod *= ( x % 10 ) ; x /= 10 ; } return prod ; } static int findNumber ( int l , int r ) { String b = Integer . toString ( r ) ; int ans = r ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { if ( b . charAt ( i ) == '0' ) continue ; char [ ] curr = b . toCharArray ( ) ; curr [ i ] = ( char ) ( ( ( int ) ( curr [ i ] - ( int ) '0' ) - 1 ) + ( int ) ( '0' ) ) ; for ( int j = i + 1 ; j < curr . length ; j ++ ) curr [ j ] = '9' ; int num = 0 ; for ( int j = 0 ; j < curr . length ; j ++ ) num = num * 10 + ( curr [ j ] - '0' ) ; if ( num >= l && product ( ans ) < product ( num ) ) ans = num ; } return ans ; } public static void main ( String [ ] args ) { int l = 1 , r = 10 ; System . out . println ( findNumber ( l , r ) ) ; l = 51 ; r = 62 ; System . out . println ( findNumber ( l , r ) ) ; } }
Concatenate suffixes of a String | Java implementation of the approach ; Function to print the expansion of the string ; Take sub - string from i to n - 1 ; Print the sub - string ; Driver code
public class GFG { static void printExpansion ( String str ) { for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { String subStr = str . substring ( i ) ; System . out . print ( subStr ) ; } } public static void main ( String args [ ] ) { String str = " geeks " ; printExpansion ( str ) ; } }
Construct a binary string following the given constraints | Java implementation of the approach ; Function to print a binary string which has ' a ' number of 0 ' s , ▁ ' b ' ▁ number ▁ of ▁ 1' s and there are at least ' x ' indices such that s [ i ] != s [ i + 1 ] ; Divide index value by 2 and store it into d ; If index value x is even and x / 2 is not equal to a ; Loop for d for each d print 10 ; subtract d from a and b ; Loop for b to print remaining 1 's ; Loop for a to print remaining 0 's ; Driver code
class GFG { static void constructBinString ( int a , int b , int x ) { int d , i ; d = x / 2 ; if ( x % 2 == 0 && x / 2 != a ) { d -- ; System . out . print ( "0" ) ; a -- ; } for ( i = 0 ; i < d ; i ++ ) System . out . print ( "10" ) ; a = a - d ; b = b - d ; for ( i = 0 ; i < b ; i ++ ) { System . out . print ( "1" ) ; } for ( i = 0 ; i < a ; i ++ ) { System . out . print ( "0" ) ; } } public static void main ( String [ ] args ) { int a = 4 , b = 3 , x = 2 ; constructBinString ( a , b , x ) ; } }
Check If every group of a ' s ▁ is ▁ followed ▁ by ▁ a ▁ group ▁ of ▁ b ' s of same length | Java implementation of the above approach ; Function to match whether there are always n consecutive b ' s ▁ followed ▁ by ▁ n ▁ consecutive ▁ a ' s throughout the string ; Traverse through the string ; Count a 's in current segment ; Count b 's in current segment ; If both counts are not same . ; Driver code
public class GFG { static boolean matchPattern ( String s ) { int count = 0 ; int n = s . length ( ) ; int i = 0 ; while ( i < n ) { while ( i < n && s . charAt ( i ) == ' a ' ) { count ++ ; i ++ ; } while ( i < n && s . charAt ( i ) == ' b ' ) { count -- ; i ++ ; } if ( count != 0 ) return false ; } return true ; } public static void main ( String [ ] args ) { String s = " bb " ; if ( matchPattern ( s ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Length of longest consecutive ones by at most one swap in a Binary String | Java program to find length of longest consecutive ones by at most one swap in a Binary String ; Function to calculate the length of the longest consecutive 1 's ; To count all 1 's in the string ; To store cumulative 1 's ; Counting cumulative 1 's from left ; If 0 then start new cumulative one from that i ; perform step 3 of the approach ; step 3 ; Driver Code ; string
import java . io . * ; class GFG { static int maximum_one ( String s , int n ) { int cnt_one = 0 ; int max_cnt = 0 , temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) { cnt_one ++ ; temp ++ ; } else { max_cnt = Math . max ( max_cnt , temp ) ; temp = 0 ; } } max_cnt = Math . max ( max_cnt , temp ) ; int [ ] left = new int [ n ] ; int right [ ] = new int [ n ] ; if ( s . charAt ( 0 ) == '1' ) left [ 0 ] = 1 ; else left [ 0 ] = 0 ; if ( s . charAt ( n - 1 ) == '1' ) right [ n - 1 ] = 1 ; else right [ n - 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) left [ i ] = left [ i - 1 ] + 1 ; else left [ i ] = 0 ; } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '1' ) right [ i ] = right [ i + 1 ] + 1 ; else right [ i ] = 0 ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( s . charAt ( i ) == '0' ) { int sum = left [ i - 1 ] + right [ i + 1 ] ; if ( sum < cnt_one ) max_cnt = Math . max ( max_cnt , sum + 1 ) ; else max_cnt = Math . max ( max_cnt , sum ) ; } } return max_cnt ; } public static void main ( String [ ] args ) { String s = "111011101" ; System . out . println ( maximum_one ( s , s . length ( ) ) ) ; } }
Maximum length substring with highest frequency in a string | Java program to find maximum occurred subString of a String ; Function to return maximum occurred subString of a String ; Size of the String ; To store maximum frequency ; To store String which has maximum frequency ; Return subString which has maximum frequency ; Driver code ; Function call
import java . util . * ; class GFG { static String MaxFreq ( String str ) { int n = str . length ( ) ; Map < String , Integer > mp = new HashMap < String , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = i ; j < n ; j ++ ) { s += str . charAt ( j ) ; if ( mp . containsKey ( s ) ) { mp . put ( s , mp . get ( s ) + 1 ) ; } else { mp . put ( s , 1 ) ; } } } int maxi = 0 ; String s = " " ; for ( Map . Entry < String , Integer > i : mp . entrySet ( ) ) { if ( i . getValue ( ) > maxi ) { maxi = i . getValue ( ) ; s = i . getKey ( ) ; } else if ( i . getValue ( ) == maxi ) { String ss = i . getKey ( ) ; if ( ss . length ( ) > s . length ( ) ) s = ss ; } } return s ; } public static void main ( String [ ] args ) { String str = " ababecdecd " ; System . out . print ( MaxFreq ( str ) ) ; } }
Lexicographically smallest substring with maximum occurrences containing a ' s ▁ and ▁ b ' s only | Java program to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only ; Function to Find the lexicographically smallest substring in a given string with maximum frequency and contains a ' s ▁ and ▁ b ' s only . ; To store frequency of digits ; size of string ; Take lexicographically larger digit in b ; get frequency of each character ; If no such string exits ; Maximum frequency ; Driver program
import java . io . * ; class GFG { static int maxFreq ( String s , int a , int b ) { int fre [ ] = new int [ 10 ] ; int n = s . length ( ) ; if ( a > b ) { int temp = a ; a = b ; b = temp ; } for ( int i = 0 ; i < n ; i ++ ) fre [ s . charAt ( i ) - '0' ] ++ ; if ( fre [ a ] == 0 && fre [ b ] == 0 ) return - 1 ; else if ( fre [ a ] >= fre [ b ] ) return a ; else return b ; } public static void main ( String [ ] args ) { int a = 4 , b = 7 ; String s = "47744" ; System . out . print ( maxFreq ( s , a , b ) ) ; } }
Minimum steps to convert one binary string to other only using negation | Java implementation of the above approach ; Function to find the minimum steps to convert string a to string b ; array to mark the positions needed to be negated ; If two character are not same then they need to be negated ; To count the blocks of 1 ; To count the number of 1 ' s ▁ in ▁ ▁ each ▁ block ▁ of ▁ 1' s ; For the last block of 1 's ; Driver code
import java . util . * ; class solution { static void convert ( int n , String a , String b ) { int [ ] l = new int [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) l [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a . charAt ( i ) != b . charAt ( i ) ) l [ i ] = 1 ; } int cc = 0 ; int vl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( l [ i ] == 0 ) { if ( vl != 0 ) cc += 1 ; vl = 0 ; } else vl += 1 ; } if ( vl != 0 ) cc += 1 ; System . out . println ( cc ) ; } public static void main ( String args [ ] ) { String a = "101010" ; String b = "110011" ; int n = a . length ( ) ; convert ( n , a , b ) ; } }
Generate a sequence with the given operations | Java Implementation of above approach ; function to find minimum required permutation ; Driver code
import java . util . * ; class GFG { static void StringMatch ( String s ) { int lo = 0 , hi = s . length ( ) , len = s . length ( ) ; Vector < Integer > ans = new Vector < > ( ) ; for ( int x = 0 ; x < len ; x ++ ) { if ( s . charAt ( x ) == ' I ' ) { ans . add ( lo ) ; lo += 1 ; } else { ans . add ( hi ) ; hi -= 1 ; } } ans . add ( lo ) ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) ) ; if ( i != ans . size ( ) - 1 ) System . out . print ( " , " ) ; } System . out . print ( " ] " ) ; } public static void main ( String [ ] args ) { String S = " IDID " ; StringMatch ( S ) ; } }
Number of ways to swap two bit of s1 so that bitwise OR of s1 and s2 changes | Java program to find no of ways to swap bits of s1 so that bitwise OR of s1 and s2 changes ; Function to find number of ways ; initialise result that store No . of swaps required ; Traverse both strings and check the bits as explained ; calculate result ; Driver code
import java . io . * ; class GFG { static int countWays ( String s1 , String s2 , int n ) { int a , b , c , d ; a = b = c = d = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s2 . charAt ( i ) == '0' ) { if ( s1 . charAt ( i ) == '0' ) { c ++ ; } else { d ++ ; } } else { if ( s1 . charAt ( i ) == '0' ) { a ++ ; } else { b ++ ; } } } result = a * d + b * c + c * d ; return result ; } public static void main ( String [ ] args ) { int n = 5 ; String s1 = "01011" ; String s2 = "11001" ; System . out . println ( countWays ( s1 , s2 , n ) ) ; } }
Find the player who rearranges the characters to get a palindrome string first | Java program to print the winner of the game ; Function that returns the winner of the game ; Initialize the freq array to 0 ; Iterate and count the frequencies of each character in the string ; Count the odd occurring character ; If odd occurrence ; Check condition for Player - 1 winning the game ; Driver code ; Function call that returns the winner
class GfG { static int returnWinner ( String s , int l ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 != 0 ) cnt ++ ; } if ( ( cnt == 0 ) || ( cnt & 1 ) == 1 ) return 1 ; else return 2 ; } public static void main ( String [ ] args ) { String s = " abaaab " ; int l = s . length ( ) ; int winner = returnWinner ( s , l ) ; System . out . println ( " Player - " + winner ) ; } }
Maximum sum and product of the M consecutive digits in a number | Java implementation of the above approach ; Function to find the maximum product ; Driver code
import java . io . * ; class GFG { static void maxProductSum ( String str , int m ) { int n = str . length ( ) ; int maxProd = Integer . MIN_VALUE , maxSum = Integer . MIN_VALUE ; for ( int i = 0 ; i <= n - m ; i ++ ) { int product = 1 , sum = 0 ; for ( int j = i ; j < m + i ; j ++ ) { product = product * ( str . charAt ( j ) - '0' ) ; sum = sum + ( str . charAt ( j ) - '0' ) ; } maxProd = Math . max ( maxProd , product ) ; maxSum = Math . max ( maxSum , sum ) ; } System . out . println ( " Maximum ▁ Product ▁ = ▁ " + maxProd ) ; System . out . print ( " Maximum Sum = " + maxSum); } public static void main ( String [ ] args ) { String str = "3605356297" ; int m = 3 ; maxProductSum ( str , m ) ; } }
Replace all consonants with nearest vowels in a string | Java program to replace all consonants with nearest vowels in a string ; Function to check if a character is vowel or not ; Function to replace consonant with nearest vowels ; if , string element is vowel , jump to next element ; check if consonant lies between two vowels , if it lies , than replace it with nearest vowel ; here the bsolute difference of ascii value is considered ; when s . charAt ( i ) is equal to either ' v ' , ' w ' , ' x ' , ' y ' , ' z ' ; Driver code
import java . util . * ; class Solution { static boolean isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) return false ; return true ; } static String replacingConsonants ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) continue ; else { if ( s . charAt ( i ) > ' a ' && s . charAt ( i ) < ' e ' ) { if ( Math . abs ( s . charAt ( i ) - ' a ' ) > Math . abs ( s . charAt ( i ) - ' e ' ) ) s = s . substring ( 0 , i ) + ' e ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' a ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' e ' && s . charAt ( i ) < ' i ' ) { if ( Math . abs ( s . charAt ( i ) - ' e ' ) > Math . abs ( s . charAt ( i ) - ' i ' ) ) s = s . substring ( 0 , i ) + ' i ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' e ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' i ' && s . charAt ( i ) < ' o ' ) { if ( Math . abs ( s . charAt ( i ) - ' i ' ) > Math . abs ( s . charAt ( i ) - ' o ' ) ) s = s . substring ( 0 , i ) + ' o ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' i ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' o ' && s . charAt ( i ) < ' u ' ) { if ( Math . abs ( s . charAt ( i ) - ' o ' ) > Math . abs ( s . charAt ( i ) - ' u ' ) ) s = s . substring ( 0 , i ) + ' u ' + s . substring ( i + 1 ) ; else s = s . substring ( 0 , i ) + ' o ' + s . substring ( i + 1 ) ; } else if ( s . charAt ( i ) > ' u ' ) s = s . substring ( 0 , i ) + ' u ' + s . substring ( i + 1 ) ; } } return s ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; System . out . print ( replacingConsonants ( s ) ) ; } }
Find time taken for signal to reach all positions in a string | Java program to Find time taken for signal to reach all positions in a string ; Returns time needed for signal to traverse through complete string . ; for the calculation of last index ; for strings like oxoooo , xoxxoooo . . ; if coun is greater than max_length ; if ' x ' is present at the right side of max_length ; if ' x ' is present at left side of max_length ; We use ceiling function to handle odd number ' o ' s ; Driver code
public class GFG { static int maxLength ( String s , int n ) { int right = 0 , left = 0 ; int coun = 0 , max_length = Integer . MIN_VALUE ; s = s + '1' ; for ( int i = 0 ; i <= n ; i ++ ) { if ( s . charAt ( i ) == ' o ' ) coun ++ ; else { if ( coun > max_length ) { right = 0 ; left = 0 ; if ( s . charAt ( i ) == ' x ' ) right = 1 ; if ( ( ( i - coun ) > 0 ) && ( s . charAt ( i - coun - 1 ) == ' x ' ) ) left = 1 ; coun = ( int ) Math . ceil ( ( double ) coun / ( right + left ) ) ; max_length = Math . max ( max_length , coun ) ; } coun = 0 ; } } return max_length ; } public static void main ( String args [ ] ) { String s = " oooxoooooooooxooo " ; int n = s . length ( ) ; System . out . println ( maxLength ( s , n ) ) ; } }