codeparrot/xlcost-text-to-code huggingface dataset:
Check if there exists a permutation of given string which doesn 't contain any monotonous substring | Java implementation such that there are no monotonous string in given string ; Function to check a string doesn 't contains a monotonous substring ; Loop to iterate over the string and check that it doesn 't contains  the monotonous substring ; Function to check that there exist a arrangement of string such that it doesn 't contains monotonous substring ; Loop to group the characters of the string into two buckets ; Sorting the two buckets ; Condition to check if the concatenation point doesn 't  contains the monotonous string ; Driver Code
import java . io . * ; import java . util . * ; class GFG { static boolean check ( String s ) { boolean ok = true ; for ( int i = 0 ; i + 1 < s . length ( ) ; ++ i ) ok &= ( Math . abs ( s . charAt ( i ) - s . charAt ( i + 1 ) ) != 1 ) ; return ok ; } static String monotonousString ( String s ) { String odd = " " , even = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) % 2 == 0 ) odd += s . charAt ( i ) ; else even += s . charAt ( i ) ; } char oddArray [ ] = odd . toCharArray ( ) ; Arrays . sort ( oddArray ) ; odd = new String ( oddArray ) ; char evenArray [ ] = even . toCharArray ( ) ; Arrays . sort ( evenArray ) ; even = new String ( evenArray ) ; if ( check ( odd + even ) ) return " Yes " ; else if ( check ( even + odd ) ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { String str = " abcd " ; String ans ; ans = monotonousString ( str ) ; System . out . println ( ans ) ; } }
Length of the smallest substring which contains all vowels | Java program to find the length of the smallest subString of which contains all vowels ; Function to return the index for respective vowels to increase their count ; Returns - 1 for consonants ; Function to find the minimum length ; Store the starting index of the current subString ; Store the frequencies of vowels ; If the current character is a vowel ; Increase its count ; Move start as much right as possible ; Condition for valid subString ; Driver code
import java . util . * ; class GFG { static int get_index ( char ch ) { if ( ch == ' a ' ) return 0 ; else if ( ch == ' e ' ) return 1 ; else if ( ch == ' i ' ) return 2 ; else if ( ch == ' o ' ) return 3 ; else if ( ch == ' u ' ) return 4 ; else return - 1 ; } static int findMinLength ( String s ) { int n = s . length ( ) ; int ans = n + 1 ; int start = 0 ; int count [ ] = new int [ 5 ] ; for ( int x = 0 ; x < n ; x ++ ) { int idx = get_index ( s . charAt ( x ) ) ; if ( idx != - 1 ) { count [ idx ] ++ ; } int idx_start = get_index ( s . charAt ( start ) ) ; while ( idx_start == - 1 count [ idx_start ] > 1 ) { if ( idx_start != - 1 ) { count [ idx_start ] -- ; } start ++ ; if ( start < n ) idx_start = get_index ( s . charAt ( start ) ) ; } if ( count [ 0 ] > 0 && count [ 1 ] > 0 && count [ 2 ] > 0 && count [ 3 ] > 0 && count [ 4 ] > 0 ) { ans = Math . min ( ans , x - start + 1 ) ; } } if ( ans == n + 1 ) return - 1 ; return ans ; } public static void main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; System . out . print ( findMinLength ( s ) ) ; } }
Number of strings which starts and ends with same character after rotations | Java program for the above approach ; Function to find the count of string with equal end after rotations ; To store the final count ; Traverse the string ; If current character is same as the previous character then increment the count ; Return the final count ; Driver Code ; Given string ; Function call
class GFG { static int countStrings ( String s ) { int cnt = 0 ; for ( int i = 1 ; i < s . length ( ) - 1 ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { String str = " aacbb " ; System . out . println ( countStrings ( str ) ) ; } }
Check if frequency of each character is equal to its position in English Alphabet | Java program for the above approach ; Initialise frequency array ; Traverse the String ; Update the frequency ; Check for valid String ; If frequency is non - zero ; If freq is not equals to ( i + 1 ) , then return false ; Return true ; ; Driver Code ; Given String str
class GFG { static boolean checkValidString ( String str ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 ) { if ( freq [ i ] != i + 1 ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { String str = " abbcccdddd " ; if ( checkValidString ( str ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
Print all the non | Java program to print all the non - repeating words from the two given sentences ; Function to print all the non - repeating words from the two given sentences ; Concatenate the two Strings into one ; Iterating over the whole concatenated String ; Searching for the word in A . If while searching , we reach the end of the String , then the word is not present in the String ; Initialise word for the next iteration ; Driver code
class GFG { static void removeRepeating ( String s1 , String s2 ) { String s3 = s1 + " ▁ " + s2 + " ▁ " ; String words = " " ; int i = 0 ; for ( char x : s3 . toCharArray ( ) ) { if ( x == ' ▁ ' ) { if ( ! s1 . contains ( words ) || ! s2 . contains ( words ) ) System . out . print ( words ) ; words = " ▁ " ; } else { words = words + x ; } } } public static void main ( String [ ] args ) { String s1 = " I ▁ have ▁ go ▁ a ▁ pen " ; String s2 = " I ▁ want ▁ to ▁ go ▁ park " ; removeRepeating ( s1 , s2 ) ; } }
Convert the number from International system to Indian system | Java program to convert the number from International system to Indian system ; Function to convert a number represented in International numeric system to Indian numeric system . ; Find the length of the sbInput ; Removing all the separators ( , ) from the sbInput ; Reverse the sbInput ; Declaring the output ; Process the sbInput ; Add a separator ( , ) after the third number ; Then add a separator ( , ) after every second number ; Reverse the output ; Return the output string back to the main function ; Driver code
import java . util . * ; class GFG { static String convert ( String input ) { StringBuilder sbInput = new StringBuilder ( input ) ; int len = sbInput . length ( ) ; for ( int i = 0 ; i < len ; ) { if ( sbInput . charAt ( i ) == ' , ' ) { sbInput . deleteCharAt ( i ) ; len -- ; i -- ; } else if ( sbInput . charAt ( i ) == ' ▁ ' ) { sbInput . deleteCharAt ( i ) ; len -- ; i -- ; } else { i ++ ; } } StringBuilder sbInputReverse = sbInput . reverse ( ) ; StringBuilder output = new StringBuilder ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( i == 2 ) { output . append ( sbInputReverse . charAt ( i ) ) ; output . append ( " ▁ , " ) ; } else if ( i > 2 && i % 2 == 0 && i + 1 < len ) { output . append ( sbInputReverse . charAt ( i ) ) ; output . append ( " ▁ , " ) ; } else { output . append ( sbInputReverse . charAt ( i ) ) ; } } StringBuilder reverseOutput = output . reverse ( ) ; return reverseOutput . toString ( ) ; } public static void main ( String [ ] args ) { String input1 = "123 , ▁ 456 , ▁ 789" ; String input2 = "90 , ▁ 050 , ▁ 000 , ▁ 000" ; System . out . println ( convert ( input1 ) ) ; System . out . println ( convert ( input2 ) ) ; } }
Construct the Cypher string based on the given conditions | Java program for the above approach ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; If the N && ( N - 2 ) is Prime ; Function to check semiPrime ; Loop from 2 to Math . sqrt ( num ) ; Increment the count of prime numbers ; If num is greater than 1 , then add 1 to it ; Return '1' if count is 2 else return '0' ; Function to make the Cypher String ; Resultant String ; Make String for the number N ; Check for semiPrime ; Traverse to make Cypher String ; If index is odd add the current character ; Else current character is changed ; Check for sum of two primes ; Traverse to make Cypher String ; If index is odd then current character is changed ; Else add the current character ; If the resultant String is " " then print - 1 ; Else print the resultant String ; Driver Code ; Given Number ; Function Call
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static boolean isPossibleSum ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) { return true ; } else { return false ; } } static boolean checkSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num /= i ; ++ cnt ; } } if ( num > 1 ) { ++ cnt ; } return cnt == 2 ; } static void makeCypherString ( int N ) { String semiPrime = " " ; String sumOfPrime = " " ; String str = String . valueOf ( N ) ; if ( checkSemiprime ( N ) ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i % 2 == 1 ) { semiPrime += str . charAt ( i ) ; } else { semiPrime += ( char ) ( str . charAt ( i ) - '0' + 65 ) ; } } } if ( isPossibleSum ( N ) ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i % 2 == 1 ) { sumOfPrime += ( char ) ( str . charAt ( i ) - '0' + 65 ) ; } else { sumOfPrime += str . charAt ( i ) ; } } } if ( semiPrime + sumOfPrime == " " ) { System . out . print ( " - 1" ) ; } else { System . out . print ( semiPrime + sumOfPrime ) ; } } public static void main ( String [ ] args ) { int N = 1011243 ; makeCypherString ( N ) ; } }
Maximum splits in binary string such that each substring is divisible by given odd number | Java code to split a given binary string into maximum possible segments divisible by given odd number K ; Function to calculate maximum splits possible ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void rearrange ( String str , int K ) { int n = str . length ( ) ; int s = 0 , sum = 0 , count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int a = str . charAt ( i ) - '0' ; sum += a * Math . pow ( 2 , s ) ; s ++ ; if ( sum != 0 && sum % K == 0 ) { count ++ ; sum = 0 ; s = 0 ; } } if ( sum != 0 ) System . out . println ( " - 1" ) ; else System . out . println ( count ) ; } public static void main ( String [ ] args ) { String str = "10111001" ; int K = 5 ; rearrange ( str , K ) ; } }
Make the string lexicographically smallest and non palindromic by replacing exactly one character | Java program to make the string lexicographically smallest non palindromic string by replacing exactly one character ; Function to find the required string ; length of the string ; Iterate till half of the string ; replacing a non ' a ' char with ' a ' ; Check if there is no ' a ' in string we replace last char of string by ' b ' ; If the input is a single character ; Driver code
import java . util . * ; class GFG { static String findStr ( String S ) { StringBuilder sb = new StringBuilder ( S ) ; int n = sb . length ( ) ; for ( int i = 0 ; i < n / 2 ; ++ i ) { if ( sb . charAt ( i ) != ' a ' ) { sb . setCharAt ( i , ' a ' ) ; return sb . toString ( ) ; } } sb . setCharAt ( n - 1 , ' b ' ) ; return n < 2 ? " ▁ - 1 ▁ " : sb . toString ( ) ; } public static void main ( String [ ] args ) { String str = " a " ; System . out . println ( findStr ( str ) ) ; String str1 = " abccba " ; System . out . println ( findStr ( str1 ) ) ; } }
Find the k | Java program find the Kth String in lexicographical order consisting of N - 2 xs and 2 ys ; Function to find the Kth String in lexicographical order which consists of N - 2 xs and 2 ys ; Iterate for all possible positions of eft most Y ; If i is the left most position of Y ; Put Y in their positions ; Driver code ; Function call
class GFG { static void kth_String ( int n , int k ) { for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( k <= ( n - i - 1 ) ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == i j == n - k ) System . out . print ( ' Y ' ) ; else System . out . print ( ' X ' ) ; } break ; } k -= ( n - i - 1 ) ; } } public static void main ( String [ ] args ) { int n = 5 , k = 7 ; kth_String ( n , k ) ; } }
Transform string str1 into str2 by taking characters from string str3 | Java program of the above approach ; Function to check whether str1 can be transformed to str2 ; To store the frequency of characters of String str3 ; Declare two pointers & flag ; Traverse both the String and check whether it can be transformed ; If both pointers point to same characters increment them ; If the letters don 't match check  if we can find it in String C ; If the letter is available in String str3 , decrement it 's  frequency & increment the ptr2 ; If letter isn 't present in str3[]  set the flag to false and break ; If the flag is true and both pointers points to their end of respective Strings then it is possible to transformed str1 into str2 , otherwise not . ; Driver Code ; Function Call
import java . util . * ; class GFG { static void convertString ( String str1 , String str2 , String str3 ) { HashMap < Character , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < str3 . length ( ) ; i ++ ) { if ( freq . containsKey ( str3 . charAt ( i ) ) ) freq . put ( str3 . charAt ( i ) , freq . get ( str3 . charAt ( i ) ) + 1 ) ; else freq . put ( str3 . charAt ( i ) , 1 ) ; } int ptr1 = 0 ; int ptr2 = 0 ; boolean flag = true ; while ( ptr1 < str1 . length ( ) && ptr2 < str2 . length ( ) ) { if ( str1 . charAt ( ptr1 ) == str2 . charAt ( ptr2 ) ) { ptr1 ++ ; ptr2 ++ ; } else { if ( freq . containsKey ( str3 . charAt ( ptr2 ) ) ) if ( freq . get ( str3 . charAt ( ptr2 ) ) > 0 ) { freq . put ( str3 . charAt ( ptr2 ) , freq . get ( str3 . charAt ( ptr2 ) ) - 1 ) ; ptr2 ++ ; } else { flag = false ; break ; } } } if ( flag && ptr1 == str1 . length ( ) && ptr2 == str2 . length ( ) ) { System . out . print ( " YES " + "NEW_LINE"); } else { System . out . print ( " NO " + "NEW_LINE"); } } public static void main ( String [ ] args ) { String str1 = " abyzfe " ; String str2 = " abcdeyzf " ; String str3 = " popode " ; convertString ( str1 , str2 , str3 ) ; } }
Decrypt the String according to given algorithm | Java implementation of the approach ; Function that returns ( num % 26 ) ; Initialize result ; One by one process all digits of ' num ' ; Function to return the decrypted String ; To store the final decrypted answer ; One by one check for each character if it is a numeric character ; Modulo the number found in the String by 26 ; Driver code ; Print the decrypted string
import java . util . * ; class GFG { static int MOD = 26 ; static int modulo_by_26 ( String num ) { int res = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { res = ( ( res * 10 + ( int ) num . charAt ( i ) - '0' ) % MOD ) ; } return res ; } static String decrypt_message ( String s ) { String decrypted_str = " " ; String num_found_so_far = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) >= '0' && s . charAt ( i ) <= '9' ) { num_found_so_far += s . charAt ( i ) ; } else if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ( char ) ( ' a ' + modulo_by_26 ( num_found_so_far ) ) ; num_found_so_far = " " ; } } if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ( char ) ( ' a ' + modulo_by_26 ( num_found_so_far ) ) ; } return decrypted_str ; } public static void main ( String [ ] args ) { String s = "32ytAAcV4ui30hf10hj18" ; System . out . print ( decrypt_message ( s ) ) ; } }
Maximum number of overlapping string | Java implementation to find the maximum number of occurrence of the overlapping count ; Function to find the maximum overlapping Strings ; Get the current character ; Condition to check if the current character is the first character of the String T then increment the overlapping count ; Condition to check previous character is also occurred ; Update count of previous and current character ; Condition to check the current character is the last character of the String T ; Condition to check the every subsequence is a valid String T ; Driver code ; Function call
import java . util . * ; class GFG { static int maxOverlap ( String S , String T ) { String str = T ; int count [ ] = new int [ T . length ( ) ] ; int overlap = 0 ; int max_overlap = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { int index = str . indexOf ( S . charAt ( i ) ) ; if ( index == 0 ) { overlap ++ ; if ( overlap >= 2 ) max_overlap = Math . max ( overlap , max_overlap ) ; count [ index ] ++ ; } else { if ( count [ index - 1 ] <= 0 ) return - 1 ; count [ index ] ++ ; count [ index - 1 ] -- ; } if ( index == 4 ) overlap -- ; } if ( overlap == 0 ) return max_overlap ; else return - 1 ; } public static void main ( String [ ] args ) { String S = " chcirphirp " ; String T = " chirp " ; System . out . print ( maxOverlap ( S , T ) ) ; } }
Minimum number of operations required to make two strings equal | Java implementation to find the minimum number of operations to make two Strings equal ; Function to find out parent of an alphabet ; Function to merge two different alphabets ; Merge a and b using rank compression ; Function to find the minimum number of operations required ; Initializing parent to i and rank ( size ) to 1 ; We will store our answer in this vector ; Traversing Strings ; If they have different parents ; Find their respective parents and merge them ; Store this in our Answer vector ; Number of operations ; Driver Code ; Two Strings S1 and S2 ; Function call
import java . util . * ; class GFG { static final int MAX = 500001 ; static int [ ] parent = new int [ MAX ] ; static int [ ] Rank = new int [ MAX ] ; static class pair { char first , second ; public pair ( char first , char second ) { this . first = first ; this . second = second ; } } static int find ( int x ) { return parent [ x ] = parent [ x ] == x ? x : find ( parent [ x ] ) ; } static void merge ( int r1 , int r2 ) { if ( r1 != r2 ) { if ( Rank [ r1 ] > Rank [ r2 ] ) { parent [ r2 ] = r1 ; Rank [ r1 ] += Rank [ r2 ] ; } else { parent [ r1 ] = r2 ; Rank [ r2 ] += Rank [ r1 ] ; } } } static void minimumOperations ( String s1 , String s2 ) { for ( int i = 1 ; i <= 26 ; i ++ ) { parent [ i ] = i ; Rank [ i ] = 1 ; } Vector < pair > ans = new Vector < pair > ( ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) != s2 . charAt ( i ) ) { if ( find ( s1 . charAt ( i ) - 96 ) != find ( s2 . charAt ( i ) - 96 ) ) { int x = find ( s1 . charAt ( i ) - 96 ) ; int y = find ( s2 . charAt ( i ) - 96 ) ; merge ( x , y ) ; ans . add ( new pair ( s1 . charAt ( i ) , s2 . charAt ( i ) ) ) ; } } } System . out . print ( ans . size ( ) + "NEW_LINE"); for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) . first + " - > " + ans . get ( i ) . second + "NEW_LINE"); } public static void main ( String [ ] args ) { String s1 , s2 ; s1 = " abb " ; s2 = " dad " ; minimumOperations ( s1 , s2 ) ; } }
Find the N | Java program to find the N - th lexicographic permutation of String using Factroid method ; Function to calculate nth permutation of String ; Creating an empty stack ; Subtracting 1 from N because the permutations start from 0 in factroid method ; Loop to generate the factroid of the sequence ; Loop to generate nth permutation ; Remove 1 - element in each cycle ; Final answer ; Driver code
import java . util . * ; class GFG { static void String_permutation ( int n , String str ) { Stack < Integer > s = new Stack < Integer > ( ) ; String result = " " ; n = n - 1 ; for ( int i = 1 ; i < str . length ( ) + 1 ; i ++ ) { s . add ( n % i ) ; n = n / i ; } for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int a = s . peek ( ) ; result += str . charAt ( a ) ; int j ; for ( j = a ; j < str . length ( ) - 1 ; j ++ ) str = str . substring ( 0 , j ) + str . charAt ( j + 1 ) + str . substring ( j + 1 ) ; str = str . substring ( 0 , j + 1 ) ; s . pop ( ) ; } System . out . print ( result + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " abcde " ; int n = 11 ; String_permutation ( n , str ) ; } }
Defanged Version of Internet Protocol Address | Java implementation to find the defanged version of the IP address ; Function to generate a defanged version of IP address . ; Loop to iterate over the characters of the string ; Driver Code
class GFG { static String GeberateDefangIP ( String str ) { String defangIP = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char c = str . charAt ( i ) ; if ( c == ' . ' ) { defangIP += " [ . ] " ; } else { defangIP += c ; } } return defangIP ; } public static void main ( String [ ] args ) { String str = "255.100.50.0" ; System . out . println ( GeberateDefangIP ( str ) ) ; } }
Generate a string with maximum possible alphabets with odd frequencies | Java program to generate a string of length n with maximum possible alphabets with each of them occuring odd number of times . ; Function to generate a string of length n with maximum possible alphabets each occuring odd number of times . ; If n is odd ; Add all characters from b - y ; Append a to fill the remaining length ; If n is even ; Add all characters from b - z ; Append a to fill the remaining length ; Driver code
import java . util . * ; class GFG { static String generateTheString ( int n ) { String ans = " " ; if ( n % 2 != 0 ) { for ( int i = 0 ; i < Math . min ( n , 24 ) ; i ++ ) { ans += ( char ) ( ' b ' + i ) ; } if ( n > 24 ) { for ( int i = 0 ; i < ( n - 24 ) ; i ++ ) ans += ' a ' ; } } else { for ( int i = 0 ; i < Math . min ( n , 25 ) ; i ++ ) { ans += ( char ) ( ' b ' + i ) ; } if ( n > 25 ) { for ( int i = 0 ; i < ( n - 25 ) ; i ++ ) ans += ' a ' ; } } return ans ; } public static void main ( String [ ] args ) { int n = 34 ; System . out . println ( generateTheString ( n ) ) ; } }
Move all occurrence of letter ' x ' from the string s to the end using Recursion | Java implementation to Move all occurrence of letter x from the string s to the end using Recursion ; Function to move all ' x ' in the end ; Store current character ; Check if current character is not ' x ' ; recursive function call ; Check if current character is ' x ' ; Driver code
import java . util . * ; class GFG { static void moveAtEnd ( String s , int i , int l ) { if ( i >= l ) return ; char curr = s . charAt ( i ) ; if ( curr != ' x ' ) System . out . print ( curr ) ; moveAtEnd ( s , i + 1 , l ) ; if ( curr == ' x ' ) System . out . print ( curr ) ; return ; } public static void main ( String args [ ] ) { String s = " geekxsforgexxeksxx " ; int l = s . length ( ) ; moveAtEnd ( s , 0 , l ) ; } }
Construct a string of length L such that each substring of length X has exactly Y distinct letters | Java implementation to construct a string of length L such that each substring of length X has exactly Y distinct letters . ; Initialize p equal to the ASCII value of a ; Iterate till the length of the string ; Driver code
public class GFG { static void string ( int l , int x , int y ) { int p = 97 ; for ( int j = 0 ; j < l ; j ++ ) { char ans = ( char ) ( p + ( j % y ) ) ; System . out . print ( ans ) ; } } public static void main ( String [ ] args ) { int l = 6 ; int x = 5 ; int y = 3 ; string ( l , x , y ) ; } }
Find the final co | Java implementation of the above approach ; Function to print the final position of the point after traversing through the given directions ; Traversing through the given directions ; If its north or south the point will move left or right ; If its west or east the point will move upwards or downwards ; Returning the final position ; Driver Code
import java . util . * ; class GFG { static void finalCoordinates ( int SX , int SY , char [ ] D ) { for ( int i = 0 ; i < D . length ; i ++ ) { if ( D [ i ] == ' N ' ) SY += 1 ; else if ( D [ i ] == ' S ' ) SY -= 1 ; else if ( D [ i ] == ' E ' ) SX += 1 ; else SX -= 1 ; } String ans = ' ( ' + String . valueOf ( SX ) + ' , ' + String . valueOf ( SY ) + ' ) ' ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int SX = 2 , SY = 2 ; String D = " NSSE " ; finalCoordinates ( SX , SY , D . toCharArray ( ) ) ; } }