Skip to content

Commit e7b2993

Browse files
committed
10/13
1 parent 9ea8c05 commit e7b2993

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

java/.DS_Store

6 KB
Binary file not shown.

java/BuddyStrings.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

java/scoreOfParenthesis.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

java/shiftingLetters.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
}

0 commit comments

Comments
 (0)