File tree Expand file tree Collapse file tree 5 files changed +143
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +143
-0
lines changed Original file line number Diff line number Diff line change 1+ import scala .collection .mutable .ListBuffer
2+
3+ /**
4+ * Definition for a Node.
5+ * class Node(var _value: Int) {
6+ * var value: Int = _value
7+ * var neighbors: List[Node] = List()
8+ * }
9+ */
10+
11+ object Solution {
12+ def cloneGraph (graph : Node ): Node = {
13+ if (graph == null ) {
14+ return null
15+ }
16+ val dp = Array .fill[Node ](101 )(null )
17+ cloneGraph(dp, graph)
18+ }
19+ def cloneGraph (dp : Array [Node ], graph : Node ): Node = {
20+ if (dp(graph.value) != null ) {
21+ return dp(graph.value)
22+ }
23+ val u = Node (graph.value)
24+ dp(graph.value) = u
25+ val neighbors = ListBuffer [Node ]()
26+ graph.neighbors
27+ .foreach {
28+ neighbors += cloneGraph(dp, _)
29+ }
30+ u.neighbors ++= neighbors
31+ u
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ int longestCommonSubsequence (String text1, String text2) {
3+ final dp = List .generate (
4+ text1.length + 1 ,
5+ (_) => List .filled (text2.length + 1 , 0 ),
6+ );
7+ for (int i = 1 ; i <= text1.length; i++ ) {
8+ for (int j = 1 ; j <= text2.length; j++ ) {
9+ dp[i][j] = text1[i - 1 ] == text2[j - 1 ] ? dp[i - 1 ][j - 1 ] + 1 : max (dp[i - 1 ][j], dp[i][j - 1 ]);
10+ }
11+ }
12+ return dp[text1.length][text2.length];
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ fun characterReplacement (s : String , k : Int ): Int {
3+ val cnts = MutableList <Int >(' Z' .code - ' A' .code + 1 ) {0 }
4+ var max_cnt = 0
5+ var ans = 0
6+ var i = 0
7+ var j = 0
8+ while (j != s.length) { // T(n) = S(n) = O(n)
9+ while (j != s.length) {
10+ val diff = j - i - max_cnt
11+ if (diff > k) {
12+ break
13+ }
14+ val d = s[j].code - ' A' .code
15+ if (diff == k && max_cnt != cnts[d]) {
16+ break
17+ }
18+ j++
19+ max_cnt = maxOf(max_cnt, ++ cnts[d])
20+ }
21+ ans = maxOf(ans, j - i)
22+ val d = s[i++ ].code - ' A' .code
23+ cnts[d]--
24+ }
25+ return ans
26+ }
27+ }
Original file line number Diff line number Diff line change 1+ func countSubstrings (s string ) int {
2+ runes := []rune {} // S(n) = O(n)
3+ for _ , c := range s {
4+ runes = append (runes , c )
5+ runes = append (runes , '\000' )
6+ }
7+ ans := 0
8+ dp := make ([]int , len (runes ) - 1 )
9+ lastR := - 1
10+ lastMid := - 1
11+ i := 0
12+ for i != len (dp ) { // Manacher T(n) = O(n)
13+ diff := lastR - i
14+ deviation := 0
15+ if diff > 0 {
16+ deviation = min (diff , dp [lastMid - (i - lastMid )])
17+ }
18+ l := i - deviation
19+ r := i + deviation
20+ for l != 0 && r + 1 != len (dp ) && runes [l - 1 ] == runes [r + 1 ] {
21+ deviation ++
22+ l --
23+ r ++
24+ }
25+ dp [i ] = deviation
26+ if r > lastR {
27+ lastR = r
28+ lastMid = i
29+ }
30+ if runes [i ] == '\000' {
31+ ans += (deviation + 1 ) >> 1
32+ } else {
33+ ans += 1 + (deviation >> 1 )
34+ }
35+ i ++
36+ }
37+ return ans
38+ }
Original file line number Diff line number Diff line change 1+ use std:: sync:: OnceLock ;
2+
3+ static _TABLE: OnceLock < Vec < i32 > > = OnceLock :: new ( ) ;
4+
5+ impl Solution {
6+
7+ pub fn reverse_bits ( mut n : i32 ) -> i32 {
8+ let mut x = 0 ;
9+ for i in 0 ..6 {
10+ let shift = if i == 5 { 2 } else { 6 } ;
11+ x = x << shift | Self :: init_table ( ) [ ( n & ( 1 << shift) - 1 ) as usize ] >> 6 - shift;
12+ n >>= 6 ;
13+ }
14+ x as i32
15+ }
16+
17+ fn init_table ( ) -> & ' static Vec < i32 > {
18+ _TABLE. get_or_init ( || {
19+ let mut table: Vec < i32 > = vec ! [ 0 ; 1 << 6 ] ;
20+ for ( i, x) in table. iter_mut ( ) . enumerate ( ) {
21+ let mut j = i as i32 ;
22+ for k in 0 ..6 {
23+ * x = * x << 1 | j & 1 ;
24+ j >>= 1 ;
25+ }
26+ }
27+ table
28+ } )
29+ }
30+
31+ }
You can’t perform that action at this time.
0 commit comments