Skip to content

Commit

Permalink
Create: 0093-restore-ip-addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifhanIlgaz committed Jan 21, 2023
1 parent ee7a3bc commit 56e63be
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
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;
}

0 comments on commit 56e63be

Please sign in to comment.