Skip to content

Create: 0093-restore-ip-addresses #2117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions go/0093-restore-ip-addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import "strconv"

func main() {

}

func restoreIpAddresses(s string) []string {
res := []string{}

if len(s) > 12 {
return res
}

var backtrack func(i, dots int, currentIP string)

backtrack = func(i, dots int, currentIP string) {
if dots == 4 && i == len(s) {
res = append(res, currentIP[:len(currentIP)-1])
return
} else if dots > 4 {
return
}


for j := i; j < min(i+3, len(s)); j++ {
val, _ := strconv.Atoi(s[i:j+1])
if val < 256 && (i == j || s[i] != '0') {
backtrack(j+1 , dots + 1, currentIP + s[i:j+1] + ".")
}
}
}

backtrack(0,0,"")
return res
}

func min(a, b int) int {
if a < b {
return a
}
return b
}
38 changes: 38 additions & 0 deletions javascript/0093-restore-ip-addresses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param {string} s
* @return {string[]}
*/
var restoreIpAddresses = function (s) {
let res = [];

if (s.length > 12) return res;

/**
*
* @param {number} i
* @param {number} dots
* @param {string} currentIP
*/
function backtracking(i, dots, currentIP) {
if (dots === 4 && i == s.length) {
res.push(currentIP.slice(0, currentIP.length - 1));
return;
} else if (dots > 4) {
return;
}

for (let j = i; j < Math.min(i + 3, s.length); j++) {
if (+s.slice(i, j + 1) < 256 && (i == j || s[i] != '0')) {
backtracking(
j + 1,
dots + 1,
currentIP + s.slice(i, j + 1) + '.'
);
}
}
}

backtracking(0, 0, '');

return res;
};
36 changes: 36 additions & 0 deletions rust/0093-restore-ip-addresses.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
impl Solution {
pub fn restore_ip_addresses(s: String) -> Vec<String> {
let mut res = vec![];

if s.len() > 12 {
return res;
}

Self::backtrack(&mut res, &s, 0, 0, "".to_string());

res
}

pub fn backtrack(res: &mut Vec<String>, s: &String, i: usize, dots: i32, current_ip: String) {
if dots == 4 && i == s.len() {
let new_valid_ip = current_ip.get(..current_ip.len() - 1).unwrap().to_string();
res.push(new_valid_ip);
return;
} else if dots > 4 {
return;
}

for j in i..usize::min(i + 3, s.len()) {
let mut val = 0;

if let Some(v) = s.get(i..j + 1) {
val = v.parse::<u32>().unwrap();
}

if val < 256 && (i == j || s.get(i..i + 1).unwrap() != "0") {
let new_ip = format!("{}{}.", current_ip, val);
Self::backtrack(res, s, j + 1, dots + 1, new_ip);
}
}
}
}
24 changes: 24 additions & 0 deletions typescript/0093-restore-ip-addresses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function restoreIpAddresses(s: string): string[] {
let result: string[] = [];

if (s.length > 12) return result;

function backtrack(i: number, dots: number, currentIP: string) {
if (dots == 4 && i == s.length) {
result.push(currentIP.slice(0, currentIP.length - 1));
return;
} else if (dots > 4) {
return;
}

for (let j = i; j < Math.min(i + 3, s.length); j++) {
if (parseInt(s.slice(i, j + 1)) < 256 && (i == j || s[i] != '0')) {
backtrack(j + 1, dots + 1, currentIP + s.slice(i, j + 1) + '.');
}
}
}

backtrack(0, 0, '');

return result;
}