java_dsa1 (1)
java_dsa1 (1)
gorithmic Problems
August 1, 2025
1
1 String Manipulation Problems
4 class Solution {
5 public static boolean isSubsequence ( String s , String t ) {
6 int i =0 , j =0;
7 int n1 = s . length () , n2 = t . length () ;
8 while (i < n1 && j < n2 ) {
9 if ( s . charAt ( i ) == t . charAt ( j ) ) {
10 i ++;
11 }
12 j ++;
13 }
14 return i == n1 ;
15 }
16 }
3 class Solution {
4 public boolean isAcronym ( List < String > words , String s ) {
5 String res = " " ;
6 for ( String i : words ) {
7 res = res + i . charAt (0) ;
8 }
9 return s . equals ( res ) ;
10 }
2
11 }
3
8 if ( prefix . isEmpty () ) return " " ;
9 }
10 }
11 return prefix ;
12 }
13
4 class Solution {
5 public static String frequencySort ( String s ) {
6 String ans = " " ;
7 Map < Character , Integer > tmap = new TreeMap < >() ;
8 for ( char c : s . toCharArray () ) {
9 tmap . put (c , tmap . getOrDefault (c , 0) + 1) ;
10 }
11 for ( Map . Entry < Character , Integer > ent : tmap . entrySet () ) {
12 char key = ent . getKey () ;
4
13 int val = ent . getValue () ;
14 for ( int i = 0; i < val ; i ++) {
15 ans += key ;
16 }
17 }
18 return ans ;
19 }
20
6 class Solution {
7 public static boolean wordPattern ( String pattern , String s ) {
8 s = s . trim () ;
9 String arr [] = s . split ( " \\ s + " ) ;
10 if ( pattern . length () != arr . length ) {
11 return false ;
12 }
13 Map < Character , String > hmap = new HashMap < >() ;
14 Set < String > set = new HashSet < >() ;
15 int i = 0;
16 for ( char c : pattern . toCharArray () ) {
17 String val = arr [ i ++];
18 String str = hmap . getOrDefault (c , " " ) ;
19 if ( val . equals ( str ) ) {
5
20 continue ;
21 } else if ( str . equals ( " " ) ) {
22 if ( set . contains ( val ) ) {
23 return false ;
24 }
25 hmap . put (c , val ) ;
26 set . add ( val ) ;
27 } else {
28 return false ;
29 }
30 }
31 return true ;
32 }
33
4 class Solution {
5 public static int firstUniqChar ( String s ) {
6 Map < Character , Integer > hmap = new LinkedHashMap < >() ;
7 for ( char c : s . toCharArray () ) {
8 hmap . put (c , hmap . getOrDefault (c , 0) + 1) ;
9 }
10 char ch = ’ ’;
11 for ( char c : hmap . keySet () ) {
12 if ( hmap . get ( c ) == 1) {
13 ch = c ;
14 break ;
15 }
16 }
17 for ( int i = 0; i < s . length () ; i ++) {
18 if ( s . charAt ( i ) == ch ) {
19 return i ;
20 }
21 }
22 return -1;
23 }
24
4 class Solution {
5 public static char findTheDifference ( String s , String t ) {
6 Map < Character , Integer > hmap = new LinkedHashMap < >() ;
7 for ( char c : s . toCharArray () ) {
6
8 hmap . put (c , hmap . getOrDefault (c , 0) + 1) ;
9 }
10 for ( char c : t . toCharArray () ) {
11 hmap . put (c , hmap . getOrDefault (c , 0) + 1) ;
12 }
13 for ( char c : hmap . keySet () ) {
14 if ( hmap . get ( c ) % 2 == 1) {
15 return c ;
16 }
17 }
18 return ’0 ’;
19 }
20
7
1 class Solution {
2 public static void rev ( char ch [] , int s , int end ) {
3 if ( s > end ) {
4 return ;
5 }
6 char temp = ch [ s ];
7 ch [ s ] = ch [ end ];
8 ch [ end ] = temp ;
9 s ++;
10 end - -;
11 rev ( ch , s , end ) ;
12 }
13
8
9 }
10 }
11 return x ;
12 }
13
4 class Solution {
5 public static List < Integer > findWordsContaining ( String [] words , char x ) {
6 List < Integer > list = new ArrayList < >() ;
7 int i = 0;
8 for ( String word : words ) {
9 if ( word . indexOf ( x ) != -1) {
10 list . add ( i ) ;
11 }
12 i ++;
13 }
14 return list ;
15 }
16
9
1 class Solution {
2 public static int maximum69Number ( int num ) {
3 char arr [] = String . valueOf ( num ) . toCharArray () ;
4 int n = arr . length ;
5 for ( int i = 0; i < n ; i ++) {
6 if ( arr [ i ] == ’6 ’) {
7 arr [ i ] = ’9 ’;
8 break ;
9 }
10 }
11 return Integer . parseInt ( new String ( arr ) ) ;
12 }
13
1 Array Problems
4 class Solution {
5 public static boolean isPossibleToSplit ( int [] nums ) {
6 int n = nums . length ;
7 Map < Integer , Integer > mp1 = new HashMap < >() ;
8 for ( int i : nums ) {
9 int val = mp1 . getOrDefault (i , 0) ;
10 mp1 . put (i , val + 1) ;
11 if ( mp1 . get ( i ) > 2) {
12 return false ;
13 }
14 }
15 return true ;
16 }
17
Listing 22: Check if array can be split into two distinct arrays
10
9 }
10 return max ;
11 }
12
11
20 public static void main ( String [] args ) {
21 int [] arr = {1 , 2 , 3 , 4 , 5 , 6 , 7};
22 int k = 3;
23 rotate ( arr , k ) ;
24 for ( int i : arr ) {
25 System . out . println ( i ) ;
26 }
27 }
28 }
12
4 class Solution {
5 public static int longestConsecutive ( int [] nums ) {
6 int longest = 0;
7 Set < Integer > set = new HashSet < >() ;
8 for ( int i : nums ) {
9 set . add ( i ) ;
10 }
11 for ( int i : nums ) {
12 if (! set . contains ( i - 1) ) {
13 int cnt = 1;
14 int x = i ;
15 while ( set . contains ( x + 1) ) {
16 cnt ++;
17 x ++;
18 }
19 longest = Math . max ( cnt , longest ) ;
20 }
21 }
22 return longest ;
23 }
24 }
3 class Solution {
4 public static void setZeroes ( int [][] matrix ) {
5 int m = matrix . length ;
6 int n = matrix [0]. length ;
7 int rows [] = new int [ m ];
8 int cols [] = new int [ n ];
9 for ( int i = 0; i < m ; i ++) {
10 for ( int j = 0; j < n ; j ++) {
11 if ( matrix [ i ][ j ] == 0) {
12 rows [ i ] = 1;
13 cols [ j ] = 1;
14 }
15 }
16 }
17 for ( int i = 0; i < m ; i ++) {
18 for ( int j = 0; j < n ; j ++) {
19 if ( rows [ i ] == 1 || cols [ j ] == 1) {
20 matrix [ i ][ j ] = 0;
21 }
22 }
23 }
24 }
25
13
4 class Solution {
5 public static List < Integer > spiralOrder ( int [][] matrix ) {
6 List < Integer > list = new ArrayList < >() ;
7 int m = matrix . length , n = matrix [0]. length ;
8 int left = 0 , top = 0 , right = n - 1 , bottom = m - 1;
9 while ( left <= right && top <= bottom ) {
10 for ( int i = left ; i <= right ; i ++) {
11 list . add ( matrix [ top ][ i ]) ;
12 }
13 top ++;
14 for ( int i = top ; i <= bottom ; i ++) {
15 list . add ( matrix [ i ][ right ]) ;
16 }
17 right - -;
18 if ( top <= bottom ) {
19 for ( int i = right ; i >= left ; i - -) {
20 list . add ( matrix [ bottom ][ i ]) ;
21 }
22 bottom - -;
23 }
24 if ( left <= right ) {
25 for ( int i = bottom ; i >= top ; i - -) {
26 list . add ( matrix [ i ][ left ]) ;
27 }
28 left ++;
29 }
30 }
31 return list ;
32 }
33
14
startsectionsubsection2@−3explus − 1exminus − .2ex1.5explus.2ex∗StringM atching(KM P )T hismethodf indsthef irstoccu
1 class Solution {
2 public static int strStr ( String haystack , String needle ) {
3 int n = haystack . length () , m = needle . length () ;
4 if ( m == 0) return 0;
5 for ( int i = 0; i <= n - m ; i ++) {
6 if ( haystack . substring (i , i + m ) . equals ( needle ) ) {
7 return i ;
8 }
9 }
10 return -1;
11 }
12
15
18 public static void main ( String [] args ) {
19 int arr [] = { -4 , -1 , 0 , 3 , 10};
20 for ( int i : sortedSquares ( arr ) ) {
21 System . out . print ( i + " " ) ;
22 }
23 }
24 }
6 class Solution {
7 public static int [] intersection ( int [] nums1 , int [] nums2 ) {
8 Set < Integer > set = new HashSet < >() ;
9 List < Integer > result = new ArrayList < >() ;
10 for ( int num : nums1 ) {
11 set . add ( num ) ;
12 }
13 for ( int num : nums2 ) {
14 if ( set . contains ( num ) ) {
15 result . add ( num ) ;
16 set . remove ( num ) ;
17 }
18 }
19 int [] res = new int [ result . size () ];
20 for ( int i = 0; i < result . size () ; i ++) {
21 res [ i ] = result . get ( i ) ;
22 }
23 return res ;
24 }
25 }
16
23 }
4 class Solution {
5 public static int thirdMax ( int [] nums ) {
6 PriorityQueue < Integer > pq = new PriorityQueue < >() ;
7 HashSet < Integer > set = new HashSet < >() ;
8 for ( int num : nums ) {
9 if ( set . add ( num ) ) {
10 pq . offer ( num ) ;
11 if ( pq . size () > 3) {
12 pq . poll () ;
13 }
14 }
15 }
16 if ( pq . size () < 3) {
17 while ( pq . size () > 1) {
18 pq . poll () ;
19 }
20 return pq . peek () ;
21 }
22 return pq . peek () ;
23 }
24
17
1 import java . util . ArrayList ;
2 import java . util . HashMap ;
3 import java . util . List ;
4 import java . util . Map ;
5
6 class Solution {
7 public static List < Integer > f in dD isa pp ea red Nu mb ers ( int [] nums ) {
8 List < Integer > list = new ArrayList < >() ;
9 Map < Integer , Integer > hmap = new HashMap < >() ;
10 int l = 1 , h = nums . length ;
11 for ( int i : nums ) {
12 hmap . put (i , hmap . getOrDefault (i , 0) + 1) ;
13 }
14 for ( int i = l ; i <= h ; i ++) {
15 if ( hmap . getOrDefault (i , 0) == 0) {
16 list . add ( i ) ;
17 }
18 }
19 return list ;
20 }
21
18
8 neg ++;
9 }
10 }
11 return Math . max ( neg , pos ) ;
12 }
13 }
4 class Solution {
5 public int getCommon ( int [] nums1 , int [] nums2 ) {
6 Set < Integer > set = new HashSet < >() ;
7 for ( int i : nums1 ) {
8 set . add ( i ) ;
9 }
10 for ( int i : nums2 ) {
11 if ( set . contains ( i ) ) {
12 return i ;
13 }
14 }
15 return -1;
16 }
17 }
19
startsectionsubsection2@−3explus − 1exminus − .2ex1.5explus.2ex∗BaseballGameT hismethodcalculatesthetotalscoreof ab
1 import java . util . ArrayList ;
2 import java . util . List ;
3
4 class Solution {
5 public static int calPoints ( String [] operations ) {
6 int count = 0;
7 List < Integer > list = new ArrayList < >() ;
8 for ( String op : operations ) {
9 if ( op . equals ( " C " ) ) {
10 if (! list . isEmpty () ) {
11 list . remove ( list . size () - 1) ;
12 }
13 } else if ( op . equals ( " D " ) ) {
14 if (! list . isEmpty () ) {
15 list . add ( list . get ( list . size () - 1) * 2) ;
16 }
17 } else if ( op . equals ( " + " ) ) {
18 if ( list . size () >= 2) {
19 int n = list . size () ;
20 int val1 = list . get ( n - 1) ;
21 int val2 = list . get ( n - 2) ;
22 list . add ( val2 + val1 ) ;
23 }
24 } else {
25 list . add ( Integer . parseInt ( op ) ) ;
26 }
27 }
28 count = list . stream () . reduce (0 , Integer :: sum ) ;
29 return count ;
30 }
31
4 class Solution {
5 public static int numJewelsInStones ( String jewels , String stones ) {
6 int count = 0;
7 Map < Character , Integer > hmap = new HashMap < >() ;
8 for ( char c : stones . toCharArray () ) {
9 hmap . put (c , hmap . getOrDefault (c , 0) + 1) ;
10 }
11 for ( char c : jewels . toCharArray () ) {
12 int val = hmap . getOrDefault (c , 0) ;
13 count = count + val ;
14 }
15 return count ;
16 }
17
20
Listing 46: Count jewels in stones
4 class Solution {
5 public static List < String > fizzBuzz ( int n ) {
6 List < String > list = new ArrayList < >() ;
7 for ( int i = 1; i <= n ; i ++) {
8 if ( i % 3 == 0 && i % 5 == 0) {
9 list . add ( " FizzBuzz " ) ;
10 } else if ( i % 3 == 0) {
11 list . add ( " Fizz " ) ;
12 } else if ( i % 5 == 0) {
13 list . add ( " Buzz " ) ;
14 } else {
15 list . add ( " " + i ) ;
16 }
17 }
18 return list ;
19 }
20
3 class Solution {
4 public static boolean isValid ( String s ) {
5 Stack < Character > stack = new Stack < >() ;
6 for ( char c : s . toCharArray () ) {
7 if ( c == ’( ’ || c == ’{ ’ || c == ’[ ’) {
8 stack . push ( c ) ;
9 } else {
10 if ( stack . isEmpty () ) return false ;
11 char top = stack . pop () ;
12 if (( c == ’) ’ && top != ’( ’) ||
13 ( c == ’} ’ && top != ’{ ’) ||
14 ( c == ’] ’ && top != ’[ ’) ) {
15 return false ;
16 }
17 }
18 }
19 return stack . isEmpty () ;
20 }
21 }
21
5 if (( n & 1) == 1) {
6 count ++;
7 }
8 n = n >>> 1; // Use unsigned right shift
9 }
10 return count ;
11 }
12
4 class Solution {
5 public static boolean co nt a in s Ne ar b yD u pl ic a te ( int [] nums , int k ) {
6 Map < Integer , Integer > hmap = new HashMap < >() ;
7 for ( int i = 0; i < nums . length ; i ++) {
8 int num = nums [ i ];
9 if ( hmap . containsKey ( num ) && i - hmap . get ( num ) <= k ) {
10 return true ;
11 }
12 hmap . put ( num , i ) ;
13 }
14 return false ;
15 }
16 }
22
3 class Solution {
4 public static int maxProfit ( int [] prices ) {
5 int min = Integer . MAX_VALUE ;
6 int maxProfit = 0;
7 for ( int price : prices ) {
8 min = Math . min ( min , price ) ;
9 maxProfit = Math . max ( maxProfit , price - min ) ;
10 }
11 return maxProfit ;
12 }
13
4 class Solution {
5 public static int [] f i n d M i s s i n g A n d R e p e a t e d V a l u e s ( int [][] grid ) {
6 int n = grid . length ;
7 int arr [] = new int [2];
8 Map < Integer , Integer > hmap = new HashMap < >() ;
9 for ( int i = 0; i < n ; i ++) {
10 for ( int j = 0; j < n ; j ++) {
11 int ele = grid [ i ][ j ];
12 hmap . put ( ele , hmap . getOrDefault ( ele , 0) + 1) ;
13 }
14 }
15 for ( int i = 1; i <= n * n ; i ++) {
16 if ( hmap . getOrDefault (i , 0) == 2) {
17 arr [0] = i ;
18 } else if ( hmap . getOrDefault (i , 0) == 0) {
19 arr [1] = i ;
20 }
21 }
22 return arr ;
23 }
24
23
1 Binary Tree Problems
14 class Solution {
15 public boolean hasPathSum ( TreeNode root , int targetSum ) {
16 if ( root == null ) {
17 return false ;
18 }
19 if ( root . left == null && root . right == null ) {
20 return targetSum == root . val ;
21 }
22 return hasPathSum ( root . left , targetSum - root . val ) ||
23 hasPathSum ( root . right , targetSum - root . val ) ;
24 }
25 }
4 class Solution {
5 public static void inorder ( TreeNode root , List < Integer > list ) {
6 if ( root == null ) {
7 return ;
8 }
9 inorder ( root . left , list ) ;
10 list . add ( root . val ) ;
11 inorder ( root . right , list ) ;
12 }
13
24
startsectionsubsection2@−3explus − 1exminus − .2ex1.5explus.2ex∗ZigzagLevelOrderT raversalT hismethodperf ormsazi
ordertraversalof abinarytree.
1 import java . util . ArrayList ;
2 import java . util . Collections ;
3 import java . util . LinkedList ;
4 import java . util . List ;
5 import java . util . Queue ;
6
7 class Solution {
8 public List < List < Integer > > zigzagLevelOrder ( TreeNode root ) {
9 List < List < Integer > > list = new ArrayList < >() ;
10 if ( root == null ) {
11 return list ;
12 }
13 Queue < TreeNode > queue = new LinkedList < >() ;
14 queue . add ( root ) ;
15 int level = 0;
16 while (! queue . isEmpty () ) {
17 int size = queue . size () ;
18 List < Integer > arr = new ArrayList < >() ;
19 for ( int i = 0; i < size ; i ++) {
20 TreeNode treeNode = queue . poll () ;
21 arr . add ( treeNode . val ) ;
22 if ( treeNode . left != null ) {
23 queue . add ( treeNode . left ) ;
24 }
25 if ( treeNode . right != null ) {
26 queue . add ( treeNode . right ) ;
27 }
28 }
29 if ( level % 2 != 0) {
30 Collections . reverse ( arr ) ;
31 }
32 level ++;
33 list . add ( arr ) ;
34 }
35 return list ;
36 }
37 }
25
18 return false ;
19 }
20 boolean left = isBalanced ( root . left ) ;
21 boolean right = isBalanced ( root . right ) ;
22 return left && right ;
23 }
24 }
4 class Solution {
5 public static TreeNode searchBST ( TreeNode root , int val ) {
6 if ( root == null || root . val == val ) {
7 return root ;
8 }
9 if ( val < root . val ) {
10 return searchBST ( root . left , val ) ;
11 }
12 return searchBST ( root . right , val ) ;
26
13 }
14 }
5 class Node {
6 int data ;
7 Node left , right ;
8 public Node ( int data ) {
9 this . data = data ;
10 this . left = this . right = null ;
11 }
12 }
13
14 class Pair {
15 Node node ;
27
16 int val ;
17 public Pair ( Node node , int val ) {
18 this . node = node ;
19 this . val = val ;
20 }
21 }
22
23 class Tree {
24 public static void genDfs ( Node root ) {
25 List < Integer > pre = new ArrayList < >() ;
26 List < Integer > post = new ArrayList < >() ;
27 List < Integer > ino = new ArrayList < >() ;
28 Stack < Pair > stack = new Stack < >() ;
29 stack . push ( new Pair ( root , 1) ) ;
30 while (! stack . isEmpty () ) {
31 Pair pair = stack . peek () ;
32 if ( pair . val == 1) {
33 pre . add ( pair . node . data ) ;
34 pair . val ++;
35 if ( pair . node . left != null ) {
36 stack . add ( new Pair ( pair . node . left , 1) ) ;
37 }
38 } else if ( pair . val == 2) {
39 ino . add ( pair . node . data ) ;
40 pair . val ++;
41 if ( pair . node . right != null ) {
42 stack . add ( new Pair ( pair . node . right , 1) ) ;
43 }
44 } else {
45 post . add ( pair . node . data ) ;
46 stack . pop () ;
47 }
48 }
49 System . out . println ( " PREORDER IS " + pre ) ;
50 System . out . println ( " INORDER IS " + ino ) ;
51 System . out . println ( " POSTORDER IS " + post ) ;
52 }
53
4 class ListNode {
28
5 int val ;
6 ListNode next ;
7 ListNode () {}
8 ListNode ( int val ) { this . val = val ; }
9 ListNode ( int val , ListNode next ) { this . val = val ; this . next = next ; }
10 }
11
12 class Solution1 {
13 public static ListNode deleteDuplicates ( ListNode head ) {
14 if ( head == null ) {
15 return null ;
16 }
17 ListNode newNode = new ListNode ( -1) ;
18 Set < Integer > set = new TreeSet < >() ;
19 ListNode curr = head ;
20 while ( curr != null ) {
21 set . add ( curr . val ) ;
22 curr = curr . next ;
23 }
24 ListNode tail = newNode ;
25 for ( int i : set ) {
26 ListNode listNode = new ListNode ( i ) ;
27 tail . next = listNode ;
28 tail = tail . next ;
29 }
30 return newNode . next ;
31 }
32
1 Matrix Problems
29
15 arr [ i ] = arr [ i ] == 0 ? 1 : 0;
16 }
17 return arr ;
18 }
19
4 class Solution {
5 public static List < Integer > luckyNumbers ( int [][] matrix ) {
6 List < Integer > list = new ArrayList < >() ;
7 if ( matrix == null ) {
8 return list ;
9 }
10 int r = matrix . length , c = matrix [0]. length ;
11 for ( int i = 0; i < r ; i ++) {
12 int min = Integer . MAX_VALUE , minCol = 0;
13 for ( int j = 0; j < c ; j ++) {
14 if ( matrix [ i ][ j ] < min ) {
15 min = matrix [ i ][ j ];
16 minCol = j ;
17 }
18 }
19 boolean isMax = true ;
20 for ( int k = 0; k < r ; k ++) {
21 if ( matrix [ k ][ minCol ] > min ) {
22 isMax = false ;
23 break ;
24 }
25 }
26 if ( isMax ) {
27 list . add ( min ) ;
28 }
29 }
30 return list ;
31 }
32
30
1 Miscellaneous Problems
31