File tree Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Expand file tree Collapse file tree 4 files changed +52
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean buddyStrings (String A , String B ) {
3
+ if (A .length () != B .length ()) return false ;
4
+
5
+ if (A .equals (B )){
6
+ Set <Character > set = new HashSet <>();
7
+ for (char c :A .toCharArray ()) set .add (c );
8
+ return set .size () < A .length ();
9
+ }
10
+
11
+ List <Integer > diff = new ArrayList <>();
12
+ for (int i =0 ; i <A .length (); i ++)
13
+ if (A .charAt (i ) != B .charAt (i ))
14
+ diff .add (i );
15
+
16
+ return diff .size ()<=2 &&
17
+ A .charAt (diff .get (0 )) == B .charAt (diff .get (1 )) &&
18
+ A .charAt (diff .get (1 )) == B .charAt (diff .get (0 ));
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int scoreOfParentheses (String S ) {
3
+ Stack <Integer > stack = new Stack <>();
4
+ for (char c :S .toCharArray ()){
5
+ if (c == '(' )
6
+ stack .push (-1 );
7
+ else {
8
+ int cur = 0 ;
9
+ while (stack .peek () != -1 )
10
+ cur += stack .pop ();
11
+ stack .pop ();
12
+ stack .push (cur ==0 ? 1 :2 *cur );
13
+ }
14
+ }
15
+ int sum = 0 ;
16
+ while (!stack .isEmpty ())
17
+ sum += stack .pop ();
18
+ return sum ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String shiftingLetters (String S , int [] shifts ) {
3
+ if (shifts .length == 0 ) return S ;
4
+ char [] arr = S .toCharArray ();
5
+ int shift = 0 ;
6
+ for (int i =shifts .length -1 ; i >=0 ; i --){
7
+ shift = (shift + shifts [i ])%26 ;
8
+ arr [i ] = (char )((arr [i ] - 'a' + shift ) % 26 + 'a' );
9
+ }
10
+ return new String (arr );
11
+ }
12
+ }
You can’t perform that action at this time.
0 commit comments