codeparrot/xlcost-text-to-code huggingface dataset:
Program to construct a DFA to check if a given integer is unsigned or not | Java program for the above approach ; Function to construct DFA as per the given conditions ; If at state 0 and a digit has occurred then set it to state 1 ; Similarly for all other states ; Function to build and connect the DFA states ; Connect all the states to the dead state ; Function call to make DFA as per the given conditions ; Function call to check whether an integer in the form of string is unsigned integer or not ; Build the DFA ; Stores the current state ; Traverse the string ; If at a certain state a digit occurs then change the current state according to the DFA ; Or + / - sign ; Or decimal occurred ; Or any other character ; Or e / E or exponent sign ; State 1 , 4 , 8 will give the final answer ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String digits = "0123456789" , sign = " + - " ; static String dot = " . " , ex = " eE " ; static int dfa [ ] [ ] = new int [ 11 ] [ 5 ] ; static void makeDFA ( ) { dfa [ 0 ] [ 0 ] = 1 ; dfa [ 1 ] [ 0 ] = 1 ; dfa [ 1 ] [ 2 ] = 3 ; dfa [ 1 ] [ 3 ] = 2 ; dfa [ 1 ] [ 4 ] = 6 ; dfa [ 3 ] [ 0 ] = 4 ; dfa [ 4 ] [ 0 ] = 4 ; dfa [ 4 ] [ 3 ] = 5 ; dfa [ 4 ] [ 4 ] = 6 ; dfa [ 6 ] [ 0 ] = 8 ; dfa [ 6 ] [ 1 ] = 7 ; dfa [ 7 ] [ 0 ] = 8 ; dfa [ 8 ] [ 0 ] = 8 ; dfa [ 8 ] [ 3 ] = 9 ; } static void buildDFA ( ) { for ( int i = 0 ; i < 11 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) dfa [ i ] [ j ] = 10 ; makeDFA ( ) ; } static void checkDFA ( String s ) { buildDFA ( ) ; int currentstate = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( digits . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 0 ] ; else if ( sign . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 1 ] ; else if ( dot . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 2 ] ; else if ( ex . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 4 ] ; else currentstate = dfa [ currentstate ] [ 3 ] ; } if ( currentstate == 1 currentstate == 4 currentstate == 8 ) { System . out . println ( " Unsigned ▁ integer " ) ; } else { System . out . println ( " Not ▁ an ▁ unsigned ▁ integer " ) ; } } public static void main ( String [ ] args ) { String S = "1729" ; checkDFA ( S ) ; } }
Convert a Mobile Numeric Keypad sequence to equivalent sentence | java program for the above approach ; Function to convert mobile numeric keypad sequence into its equivalent string ; Store the mobile keypad mappings ; Traverse the string str ; If the current character is ' . ' , then continue to the next iteration ; Stores the number of continuous clicks ; Iterate a loop to find the count of same characters ; 2 , 3 , 4 , 5 , 6 and 8 keys will have maximum of 3 letters ; 7 and 9 keys will have maximum of 4 keys ; Handle the end condition ; Check if the current pressed key is 7 or 9 ; Else , the key pressed is either 2 , 3 , 4 , 5 , 6 or 8 ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printSentence ( String S ) { String nums [ ] = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; char str [ ] = S . toCharArray ( ) ; int i = 0 ; while ( i < str . length ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( i + 1 < str . length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= '2' && str [ i ] <= '6' ) || ( str [ i ] == '8' ) ) ) break ; else if ( count == 3 && ( str [ i ] == '7' str [ i ] == '9' ) ) break ; count ++ ; i ++ ; if ( i == str . length ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 4 ) ) ; } else { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 3 ) ) ; } i ++ ; } } public static void main ( String [ ] args ) { String str = "234" ; printSentence ( str ) ; } }
Lexicographically largest string with sum of characters equal to N | java program for the above approach ; Function to construct the lexicographically largest string having sum of characters as N ; Stores the resulting string ; Iterate until N is at least 26 ; Append ' z ' to the string ans ; Decrement N by 26 ; Append character at index ( N + ' a ' ) ; Return the resultant string ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static String getString ( int N ) { String ans = " " ; while ( N >= 26 ) { ans += ' z ' ; N -= 26 ; } ans += ( char ) ( N + ' a ' - 1 ) ; return ans ; } public static void main ( String [ ] args ) { int N = 30 ; System . out . print ( getString ( N ) ) ; } }
Length of all prefixes that are also the suffixes of given string | Java program for the above approach ; Function to find the length of all prefixes of the given string that are also suffixes of the same string ; Stores the prefix string ; Traverse the string S ; Add the current character to the prefix string ; Store the suffix string ; Check if both the strings are equal or not ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void countSamePrefixSuffix ( String s , int n ) { String prefix = " " ; for ( int i = 0 ; i < n - 1 ; i ++ ) { prefix += s . charAt ( i ) ; String suffix = s . substring ( n - 1 - i , n ) ; if ( prefix . equals ( suffix ) ) { System . out . print ( prefix . length ( ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String S = " ababababab " ; int N = S . length ( ) ; countSamePrefixSuffix ( S , N ) ; } }
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array | Java program for the above approach ; Function to count newly created pairs by swapping the first characters of any pairs of strings ; Stores the count all possible pair of strings ; Push all the strings into the Unordered Map ; Generate all possible pairs of strings from the array arr [ ] ; Store the current pair of strings ; Swap the first character ; Check if both string are not present in map ; Print the result ; Driver code
import java . lang . * ; import java . util . * ; class GFG { static void countStringPairs ( String a [ ] , int n ) { int ans = 0 ; Map < String , Integer > s = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . put ( a [ i ] , s . getOrDefault ( a [ i ] , 0 ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { StringBuilder p = new StringBuilder ( a [ i ] ) ; StringBuilder q = new StringBuilder ( a [ j ] ) ; if ( p . charAt ( 0 ) != q . charAt ( 0 ) ) { char t = p . charAt ( 0 ) ; p . setCharAt ( 0 , q . charAt ( 0 ) ) ; q . setCharAt ( 0 , t ) ; if ( ! s . containsKey ( p . toString ( ) ) && ! s . containsKey ( q . toString ( ) ) ) { ans ++ ; } } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " good " , " bad " , " food " } ; int N = arr . length ; countStringPairs ( arr , N ) ; } }
Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | Java program for the above approach ; Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times ; Base Case 1 ; Count of ' ( ' and ' ) ' ; Base Case 2 ; Store the count of moves required to make a valid parenthesis ; Traverse the string ; Increment cnt if opening bracket has occurred ; Otherwise , decrement cnt by 1 ; Decrement cnt by 1 ; If cnt is negative ; Update the cnt ; Increment the ans ; If ans is at most K , then print Yes . Otherwise print No ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minimumMoves ( String s , int n , int k ) { if ( n % 2 == 1 ) { System . out . println ( " No " ) ; return ; } int countOpen = 0 , countClose = 0 ; for ( char ch : s . toCharArray ( ) ) if ( ch == ' ( ' ) countOpen ++ ; else if ( ch == ' ) ' ) countClose ++ ; if ( countOpen != countClose ) { System . out . println ( " No " ) ; return ; } int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s . charAt ( i ) == ' ( ' ) ++ cnt ; else { -- cnt ; if ( cnt < 0 ) { cnt = 0 ; ++ ans ; } } } if ( ans <= k ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " ) ( " ; int K = 1 ; minimumMoves ( S , S . length ( ) , K ) ; } }
Generate a random Binary String of length N | Java program for the above approach ; Function to find a random number between 0 or 1 ; Generate the random number ; Return the generated number ; Function to generate a random binary string of length N ; Stores the empty string ; Iterate over the range [ 0 , N - 1 ] ; Store the random number ; Append it to the string ; Print the resulting string ; Driver Code
class GFG { static int findRandom ( ) { int num = ( 1 + ( int ) ( Math . random ( ) * 100 ) ) % 2 ; return num ; } static void generateBinaryString ( int N ) { String S = " " ; for ( int i = 0 ; i < N ; i ++ ) { int x = findRandom ( ) ; S = S + String . valueOf ( x ) ; } System . out . println ( S ) ; } public static void main ( String [ ] args ) { int N = 7 ; generateBinaryString ( N ) ; } }
Partition a string into palindromic strings of at least length 2 with every character present in a single string | Java program for the above approach ; Function to check if a string can be split into palindromic strings of at least length 2 by including every character exactly once ; Stores the frequency of each character ; Store the frequency of characters with frequencies 1 and even respectively ; Traverse the string s ; Iterate over all the characters ; If the frequency is 1 ; If frequency is even ; Print the result ; Stores the number of characters with frequency equal to 1 that are not part of a palindromic string ; Iterate over all the characters ; If o becomes less than 0 , then break out of the loop ; If frequency of the current character is > 2 and is odd ; Update the value of o ; If a single character is still remaining ; Increment o by 1 ; Set a [ i ] to 1 ; Print the result ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void checkPalindrome ( String s ) { int a [ ] = new int [ 26 ] ; int o = 0 , e = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) a [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( a [ i ] == 1 ) o ++ ; else if ( a [ i ] % 2 == 0 && a [ i ] != 0 ) e += ( a [ i ] / 2 ) ; } if ( e >= o ) System . out . println ( " Yes " ) ; else { o = o - e ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( o <= 0 ) break ; if ( o > 0 && a [ i ] % 2 == 1 && a [ i ] > 2 ) { int k = o ; o = o - a [ i ] / 2 ; if ( o > 0 2 * k + 1 == a [ i ] ) { o ++ ; a [ i ] = 1 ; } } } if ( o <= 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S = " abbbaddzcz " ; checkPalindrome ( S ) ; } }
Maximize time by replacing ' _ ' in a given 24 Hour format time | Java program for the above approach ; Function to find the maximum time possible by replacing each ' _ ' with any digit ; If the first character is ' _ ' ; If s [ 1 ] is ' _ ' or s [ 1 ] is less than 4 ; Update s [ 0 ] as 2 ; Otherwise , update s [ 0 ] = 1 ; If s [ 1 ] is equal to ' _ ' ; If s [ 0 ] is equal to '2' ; Otherwise ; If S [ 3 ] is equal to ' _ ' ; If s [ 4 ] is equal to ' _ ' ; Print the modified string ; Driver Code
class GFG { static void maximumTime ( String str ) { char [ ] s = str . toCharArray ( ) ; if ( s [ 0 ] == ' _ ' ) { if ( ( s [ 1 ] == ' _ ' ) || ( s [ 1 ] >= '0' && s [ 1 ] < '4' ) ) { s [ 0 ] = '2' ; } else { s [ 0 ] = '1' ; } } if ( s [ 1 ] == ' _ ' ) { if ( s [ 0 ] == '2' ) { s [ 1 ] = '3' ; } else { s [ 1 ] = '9' ; } } if ( s [ 3 ] == ' _ ' ) { s [ 3 ] = '5' ; } if ( s [ 4 ] == ' _ ' ) { s [ 4 ] = '9' ; } for ( int i = 0 ; i < s . length ; i ++ ) System . out . print ( s [ i ] ) ; } static public void main ( String [ ] args ) { String S = "0 _ : 4 _ " ; maximumTime ( S ) ; } }
Nearest power of 2 of frequencies of each digit of a given number | Java program for the above approach ; Function to find the nearest power of 2 for all frequencies in the Map freq ; Traverse the Map ; Calculate log of the current array element ; Find the nearest power of 2 for the current frequency ; Function to find nearest power of 2 for frequency of each digit of num ; Length of string ; Stores the frequency of each character in the string ; Traverse the string S ; Function call to generate nearest power of 2 for each frequency ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void nearestPowerOfTwoUtil ( HashMap < Character , Integer > freq ) { for ( char key : freq . keySet ( ) ) { System . out . print ( key + " ▁ - > ▁ " ) ; int lg = ( int ) ( Math . log ( freq . get ( key ) / Math . log ( 2 ) ) ) ; int a = ( int ) Math . pow ( 2 , lg ) ; int b = ( int ) Math . pow ( 2 , lg + 1 ) ; if ( ( freq . get ( key ) - a ) < ( b - freq . get ( key ) ) ) { System . out . println ( a ) ; } else { System . out . println ( b ) ; } } } static void nearestPowerOfTwo ( String S ) { int N = S . length ( ) ; HashMap < Character , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { freq . put ( S . charAt ( i ) , freq . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; } nearestPowerOfTwoUtil ( freq ) ; } public static void main ( String [ ] args ) { String N = "16333331163" ; nearestPowerOfTwo ( N ) ; } }
Smallest string divisible by two given strings | Java program for above approach ; Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , print - 1 ; Driver code
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int lcm ( int a , int b ) { return ( a / gcd ( a , b ) ) * b ; } static void findSmallestString ( String s , String t ) { int n = s . length ( ) , m = t . length ( ) ; int l = lcm ( n , m ) ; String s1 = " " , t1 = " " ; for ( int i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( int i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 . equals ( t1 ) ) { System . out . println ( s1 ) ; } else { System . out . println ( - 1 ) ; } } public static void main ( String [ ] args ) { String S = " baba " , T = " ba " ; findSmallestString ( S , T ) ; } }
Minimize removal of substring of 0 s to remove all occurrences of 0 s from a circular Binary String | Java program for the above approach ; Function to count minimum number of removal of consecutive 0 s required to make binary string consists only of 1 s ; Stores the count of removals ; Traverse the string S ; If the current character is '0' ; Traverse until consecutive characters are only '0' s ; If the binary string only contains 1 s , then return 1 ; If the first and the last characters are 0 ; Return the resultant count ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int minRemovals ( String str , int N ) { int ans = 0 ; boolean X = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '0' ) { ans ++ ; while ( i < N && str . charAt ( i ) == '0' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str . charAt ( 0 ) == '0' && str . charAt ( N - 1 ) == '0' ) { ans -- ; } return ans ; } public static void main ( String [ ] args ) { String S = "11010001" ; int N = S . length ( ) ; System . out . println ( minRemovals ( S , N ) ) ; } }
Minimum removals required to place all 0 s before 1 s in a Binary String | Java program to implement the above approach ; Function to count minimum removals required to arrange all 0 s before 1 s ; Count the occurences of 0 in s ; Size of the string ; Stores the minimum number of removals required ; Iterate over each of the characters in the string s ; If the i - th character is found to be '0' ; Store the minimum of res and right_0 + left_1 in res ; Return the final result ; Driver Code
import java . util . * ; class GFG { static int minimumDeletions ( String s ) { int right_0 = ( int ) ( s . chars ( ) . filter ( ch -> ch == '0' ) . count ( ) ) ; int left_1 = 0 ; int n = s . length ( ) ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '0' ) { right_0 -= 1 ; } else { left_1 += 1 ; } res = Math . min ( res , right_0 + left_1 ) ; } return res ; } public static void main ( String [ ] args ) { String s = "001101" ; int count = minimumDeletions ( s ) ; System . out . print ( count ) ; } }
Minimum substring flips required to convert a Binary String to another | Java program for the above approach ; Function to count the minimum number of reversals required to make the given binary strings s1 and s2 same ; Stores the minimum count of reversal of substrings required ; If the length of the strings are not the same then return - 1 ; Iterate over each character ; If s1 [ i ] is not equal to s2 [ i ] ; Iterate until s1 [ i ] != s2 [ i ] ; Increment answer by 1 ; Return the resultant count of reversal of substring required ; Driver Code ; Function Call
import java . io . * ; class GFG { static int canMakeSame ( String s1 , String s2 ) { int ans = 0 ; if ( s1 . length ( ) != s2 . length ( ) ) { return - 1 ; } int N = s1 . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( s1 . charAt ( i ) != s2 . charAt ( i ) ) { while ( i < s1 . length ( ) && s1 . charAt ( i ) != s2 . charAt ( i ) ) { i ++ ; } ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String S1 = "100001" ; String S2 = "110111" ; System . out . println ( canMakeSame ( S1 , S2 ) ) ; } }
Count intervals that intersects with a given meeting time | Java implementation of the above approach ; Function to convert a time in 24 hour format to an equivalent integer ; Removes " : " at 3 rd position ; Calculate hours ; Stores the time in 24 hours format ; If time is in " AM " ; If hh is equal to 12 ; If time is in " PM " ; If hh is equal to 12 ; Return time ; Function to count number of intervals in which p lies ; Stores the count ; Stores the integer value of 24 hours time format of P ; Traverse the array ; Stores the integer value of 24 hours time format of arr [ i ] [ 0 ] ; Stores the integer value of 24 hours time format of arr [ i ] [ 1 ] ; If M lies within the [ L , R ] ; Increment ans by 1 ; Return ans ; Driver Code
import java . io . * ; class GFG { static int convert ( String str ) { str = str . substring ( 0 , 2 ) + str . substring ( 3 ) ; int h1 = ( int ) str . charAt ( 1 ) - '0' ; int h2 = ( int ) str . charAt ( 0 ) - '0' ; int hh = ( h2 * 10 + h1 % 10 ) ; int time = 0 ; if ( str . charAt ( 5 ) == ' A ' ) { if ( hh == 12 ) time += Integer . parseInt ( str . substring ( 2 , 4 ) ) ; else { time += Integer . parseInt ( str . substring ( 0 , 2 ) ) ; } } else { if ( hh == 12 ) { time += Integer . parseInt ( str . substring ( 0 , 4 ) ) ; } else { time += Integer . parseInt ( str . substring ( 0 , 4 ) ) ; time += 1200 ; } } return time ; } static int countOverlap ( String arr [ ] [ ] , int n , String p ) { int ans = 0 ; int M = convert ( p ) ; for ( int i = 0 ; i < n ; i ++ ) { int L = convert ( arr [ i ] [ 0 ] ) ; int R = convert ( arr [ i ] [ 1 ] ) ; if ( ( L <= M && M <= R ) || ( M >= R && M <= L ) ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { String [ ] [ ] arr = new String [ ] [ ] { { "12:00 : AM " , "11:55 : PM " } , { "12:01 : AM " , "11:50 : AM " } , { "12:30 : AM " , "12:00 : PM " } , { "11:57 : AM " , "11:59 : PM " } } ; String P = "12:01 : PM " ; int N = arr . length ; System . out . println ( countOverlap ( arr , N , P ) ) ; } }
Count points which are revisited while following the path specified by a given string | Java program for the above approach ; Function to find the number of times already visited position is revisited after starting traversal from { X , Y } ; Stores the x and y temporarily ; Stores the number of times an already visited position is revisited ; Initialize hashset ; Insert the starting coordinates ; Traverse over the string ; Update the coordinates according to the current directions ; If the new { X , Y } has been visited before , then increment the count by 1 ; Otherwise ; Insert new { x , y } ; Driver Code
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int count ( String S , int X , int Y ) { int N = S . length ( ) ; int temp_x = 0 , temp_y = 0 ; int count = 0 ; HashSet < String > s = new HashSet < > ( ) ; s . add ( ( X + " # " + Y ) ) ; for ( int i = 0 ; i < N ; i ++ ) { temp_x = X ; temp_y = Y ; if ( S . charAt ( i ) == ' U ' ) { X ++ ; } else if ( S . charAt ( i ) == ' D ' ) { X -- ; } else if ( S . charAt ( i ) == ' R ' ) { Y ++ ; } else { Y -- ; } if ( s . contains ( ( temp_x + X ) + " # " + ( temp_y + Y ) ) ) { count ++ ; } else { s . add ( ( temp_x + X ) + " # " + ( temp_y + Y ) ) ; } } return count ; } public static void main ( String [ ] args ) { String S = " RDDUDL " ; int X = 0 , Y = 0 ; System . out . print ( count ( S , X , Y ) ) ; } }
Sum of frequencies of characters of a string present in another string | Java program for the above approach ; Function to find sum of frequencies of characters of S1 present in S2 ; Insert all characters of string S1 in the set ; Traverse the string S2 ; Check if X is present in bset or not ; Increment count by 1 ; Finally , print the count ; Driver code ; Given strings
import java . util . HashSet ; class GFG { static void countTotalFrequencies ( String S1 , String S2 ) { HashSet < Character > bset = new HashSet < Character > ( ) ; char [ ] S1arr = S1 . toCharArray ( ) ; char [ ] S2arr = S2 . toCharArray ( ) ; for ( char x : S1arr ) bset . add ( x ) ; int count = 0 ; for ( char x : S2arr ) { if ( bset . contains ( x ) ) count += 1 ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { String S1 = " geEksFOR " ; String S2 = " GeEksforgeEKS " ; countTotalFrequencies ( S1 , S2 ) ; } }
Check if characters of a string can be made non | Java program for the above approach ; Function to check if it 's possible to make the String s non decreasing ; Stores length of String ; Stores the last character that is not ' ? ' ; Traverse the String S ; If current character of the String is ' ? ' ; If s [ i ] is not ' ? ' and is less than C ; Otherwise ; Update C ; Return 1 ; Driver Code
import java . util . * ; class GFG { static int nonDecreasing ( char [ ] s ) { int size = s . length ; char c = ' a ' ; for ( int i = 0 ; i < size ; i ++ ) { if ( s [ i ] == ' ? ' ) { continue ; } else if ( ( s [ i ] != ' ? ' ) && ( s [ i ] < c ) ) { return 0 ; } else { c = s [ i ] ; } } return 1 ; } public static void main ( String [ ] args ) { String S = " abb ? xy ? " ; if ( nonDecreasing ( S . toCharArray ( ) ) == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }