Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
oneAway
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//JavaScript-C24.2.0 (SpiderMonkey) print(oneAway('pale', 'ple')); print(oneAway('pales', 'pale')); print(oneAway('pale', 'bake')); print(oneAway('pa', 'bake')); print(oneAway('pale', 'pal')); // 3 edits can be performed: insert char, remove char, and replace char // check if the 2 strings are 1 away function oneAway(str1, str2) { var longStr var shortStr if (Math.abs(str1.length - str2.length) > 1) return false; if (str1.length > str2.length) { longStr = str1; shortStr = str2; } else { longStr = str2; shortStr = str1; } let hasEdited = false; let j = 0; for (let i = 0; i < longStr.length; i++) { if (i > shortStr.length) { // only 1 remove away, return if !hasEdited return !hasEdited; } if (longStr[i] === shortStr[j]) { j++; continue; } else { // can remove i to have them match if (longStr[i+1] === shortStr[j]) { hasEdited = true; i++; j++; continue } } } return true; }
[
+
]
Show input
Absolute running time: 0.18 sec, cpu time: 0.14 sec, memory peak: 6 Mb, absolute service time: 0,19 sec
latest
|
history
true true true false true