diff --git a/src/arr.rs b/src/arr.rs index d6ac3e5..6c4493c 100644 --- a/src/arr.rs +++ b/src/arr.rs @@ -79,3 +79,126 @@ pub fn make_matrix(s: &str) -> Vec> { rows } + +pub fn make_char_matrix(s: &str) -> Vec> { + let s = s.trim(); + + // Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]" + let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') { + &s[1..s.len() - 1] + } else { + s + } + .trim(); + + if inner.is_empty() { + return Vec::new(); + } + + let bytes = inner.as_bytes(); + let mut i = 0usize; + let mut rows: Vec> = Vec::new(); + while i < bytes.len() { + // find next '[' + while i < bytes.len() && bytes[i] != b'[' { + i += 1; + } + if i >= bytes.len() { + break; + } + let start = i + 1; // byte index after '[' + i = start; + // find matching ']' + while i < bytes.len() && bytes[i] != b']' { + i += 1; + } + let end = i; // exclusive + // move i past ']' for next iteration (if in bounds) + if i < bytes.len() { + i += 1; + } + + if end <= start { + // empty row like [] + rows.push(Vec::new()); + continue; + } + + let row_str = &inner[start..end]; + let row_vec: Vec = row_str + .split(',') + .map(|x| { + let x = x.trim().trim_matches('"'); + if x == "null" || x.is_empty() { + ' ' + } else { + x.to_string().chars().nth(0).unwrap() + } + }) + .collect(); + rows.push(row_vec); + } + + rows +} +pub fn make_string_matrix(s: &str) -> Vec> { + let s = s.trim(); + + // Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]" + let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') { + &s[1..s.len() - 1] + } else { + s + } + .trim(); + + if inner.is_empty() { + return Vec::new(); + } + + let bytes = inner.as_bytes(); + let mut i = 0usize; + let mut rows: Vec> = Vec::new(); + while i < bytes.len() { + // find next '[' + while i < bytes.len() && bytes[i] != b'[' { + i += 1; + } + if i >= bytes.len() { + break; + } + let start = i + 1; // byte index after '[' + i = start; + // find matching ']' + while i < bytes.len() && bytes[i] != b']' { + i += 1; + } + let end = i; // exclusive + // move i past ']' for next iteration (if in bounds) + if i < bytes.len() { + i += 1; + } + + if end <= start { + // empty row like [] + rows.push(Vec::new()); + continue; + } + + let row_str = &inner[start..end]; + let row_vec: Vec = row_str + .split(',') + .map(|x| { + let x = x.trim().trim_matches('"'); + if x == "null" || x.is_empty() { + "".to_string() + } else { + x.to_string() + } + }) + .collect(); + rows.push(row_vec); + } + + rows +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 44e4f83..a4ee6ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,53 @@ struct Solution; mod arr; impl Solution { - pub fn count_submatrices(mut grid: Vec>, k: i32) -> i32 { + pub fn number_of_submatrices(mut grid: Vec>) -> i32 { + let mut arr = vec![vec![(0, 0); grid[0].len()]; grid.len()]; + match grid[0][0] { + '.' => arr[0][0] = (0, 0), + 'X' => arr[0][0] = (0, 1), + 'Y' => arr[0][0] = (1, 0), + _ => {} + } let mut ans = 0; - if grid[0][0] <= k { - ans += 1; - }else { - return 0; - } for i in 1..grid[0].len() { - grid[0][i] += grid[0][i-1]; - if grid[0][i] <= k { + let (mut y, mut x) = arr[0][i - 1]; + match grid[0][i] { + 'X' => x += 1, + 'Y' => y += 1, + _ => {} + } + if x == y && x != 0 { ans += 1; } + arr[0][i] = (y, x) } - for i in 1.. grid.len() { - grid[i][0] += grid[i-1][0]; - if grid[i][0] <= k { + + for i in 1..grid.len() { + let (mut y, mut x) = arr[i - 1][0]; + match grid[i][0] { + 'X' => x += 1, + 'Y' => y += 1, + _ => {} + } + arr[i][0] = (y, x); + if x == y && x != 0 { ans += 1; } - for j in 1.. grid[i].len() { - grid[i][j] += grid[i-1][j] + grid[i][j-1] - grid[i-1][j-1]; - if grid[i][j] <= k { + for j in 1..grid[i].len() { + let (ya, xa) = arr[i - 1][j]; + let (yb, xb) = arr[i][j - 1]; + let (yc, xc) = arr[i - 1][j - 1]; + let (mut x, mut y) = (xa + xb - xc, ya + yb - yc); + match grid[i][j] { + 'X' => x += 1, + 'Y' => y += 1, + _ => {} + } + if x == y && x != 0 { ans += 1; } + arr[i][j] = (y, x); } } ans @@ -31,9 +55,6 @@ impl Solution { } fn main() { - let result = Solution::count_submatrices(arr::make_matrix( - "[[7,2,9],[1,5,0],[2,6,6]]" - ), 20); - + let result = Solution::number_of_submatrices(arr::make_char_matrix(r#"[["X","Y"],["X","Y"]]"#)); println!("{:?}", result); }