You are on page 1of 3

Abberiviation using Backtracking :-

public class Main {

public static void solution(String str, String asf, int count, int pos) {

if (pos == str.length()) {

if (count == 0)

System.out.println(asf);

else

System.out.println(asf + count);

return;

char ch = str.charAt(pos);

if (count > 0) {

solution(str, asf + count + ch, 0, pos + 1);

} else {

solution(str, asf + ch, 0, pos + 1);

solution(str, asf, count + 1, pos + 1);

public static void main(String[] args) {

Scanner scn = new Scanner(System.in);

String str = scn.nextLine();

solution(str, "", 0, 0);

}
GOLD MINE 2

public class Main {

static int max = 0;

public static void getmax(int[][]arr, int i , int j , boolean[][] visited, ArrayList<Integer> bag){

if(i<0 || j<0 || i >= arr.length || j >= arr[0].length || visited[i][j] == true || arr[i][j] ==0) return;

visited[i][j] = true;

bag.add(arr[i][j]);

getmax(arr,i-1,j,visited,bag);

getmax(arr,i,j-1,visited,bag);

getmax(arr,i+1,j,visited,bag);

getmax(arr,i,j+1,visited,bag);

public static void getMaxGold(int[][] arr) {

boolean[][] visited = new boolean[arr.length][arr[0].length];

for(int i =0 ;i<arr.length;i++){

for(int j = 0 ;j<arr[0].length ;j++){

if(arr[i][j] !=0 && visited[i][j] == false){

ArrayList<Integer> bag = new ArrayList<>();

getmax(arr , i , j ,visited, bag);

int sum =0;

for(int x : bag){

sum = sum + x;

if(sum > max) max = sum;

You might also like