forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request neetcode-gh#2117 from AkifhanIlgaz/0093
Create: 0093-restore-ip-addresses
- Loading branch information
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |